有人能为我提供一些关于JAX-Rs Web服务中访问控制的指示.例如,基于用户凭证或名称或任何其他标准来限制访问.在太阳手册中找不到任何有用的信息.
先谢谢,Adhir
我的项目有一个休息界面.对于一个类,我有一个POST方法,你可以发布一个xml,我返回一个自定义响应,如:
<customResponse>Invalid email</customResponse>
如果发布的xml中的电子邮件不正确+我为不同情况定义的其他自定义消息.
对于所有这些,HTTP STATUS自动置于200(OK).有没有办法改变它?
Ps:我知道我可以抛出一个Web应用程序:
throw new WebApplicationException(Response.Status.BAD_REQUEST);
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我的自定义响应不再包括在内.
所以我只想将自定义错误+ 400作为http响应返回.
提前致谢.
评论后更新:我的方法是:
@POST
@Path("{membershipExternalId}")
@Consumes(MediaType.APPLICATION_XML)
@Produces("application/xml")
public CustomResponse invite(){ //code}
Run Code Online (Sandbox Code Playgroud)
你看,我回复了我的CUSTOM RESPONSE.如果我会返回简单的响应我可以设置STATUS但在这种情况下我看不到任何方式.
我看到很多Jersey的例子看起来像这样:
public class ItemResource {
@GET
@Path("/items")
@Produces({"text/xml", "application/json"})
public List<Item> getItems() {
List<Item> items = new ArrayList<Item>();
Item item = new Item();
item.setItemName("My Item Name!");
items.add(item);
return items;
}
}
Run Code Online (Sandbox Code Playgroud)
但是后来我无法解析Item,以及Jersey知道如何将Item转换为XML或JSON.我已经看到了非常基本的例子,只返回构造的HTML或XML的字符串,这对我来说更有意义,但我错过了下一步.我查看了样本,其中一个突出(json-from-jaxb样本),因为对象标有这些类型的注释:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"flight"
})
@XmlRootElement(name = "flights")
Run Code Online (Sandbox Code Playgroud)
我正在寻找一步一步涵盖这个"翻译"的教程,或者解释如何将POJO翻译为输出为特定的mime类型.谢谢!
我最近实施了Jersey JAX-RS Rest服务.我创建了一个JIBX提供程序,允许用户在XML和Java类型之间解组和编组.我还想通过在URL路径中指定版本来对我的服务进行版本控制.版本控制将包括用于编组和解组Java类型的消息绑定版本.
因此,必须将版本传递给自定义JIBX提供程序,因此必须传递包含该版本的URL路径.但是,Provider接口(MessageBodyWriter和MessageBodyReader)不在其接口方法中提供URI路径.
以下是接口writeTo()方法的方法签名MessageBodyWriter:
writeTo(Object, Type, Annotation[], MediaType, MultivaluedMap, OutputStream)
Run Code Online (Sandbox Code Playgroud)
此方法参数不包含路径uri,因此,自定义jibx提供程序无法知道应该使用哪个消息绑定版本来编组Java类型.有没有解决的办法?
Jersey通过查看accept标头来识别请求.我有一个只接受text/*的请求 - 我如何强制响应是例如application/json?
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
public MyResponseObject create() {
return new MyResponseObject();
}
Run Code Online (Sandbox Code Playgroud)
如果请求被指向创建只接受text/*jersey将返回500.有没有办法解决这个问题?(我无法更改请求接受标头).
我有一个JAX-RS WebService,使用以下方法:
@Path("/myrest")
public class MyRestResource {
...
@GET
@Path("/getInteger")
@Produces(APPLICATION_JSON)
public Integer getInteger() {
return 42;
}
Run Code Online (Sandbox Code Playgroud)
使用此剪切访问时:
@Test
public void testGetPrimitiveWrapers() throws IOException {
// this works:
assertEquals(new Integer(42), new ObjectMapper().readValue("42", Integer.class));
// that fails:
assertEquals(new Integer(42), resource().path("/myrest/getInteger").get(Integer.class));
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class java.lang.Integer, and Java type class java.lang.Integer, and MIME media type application/json was not found
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用RESTEasy和Weld进行注入的REST Web服务.当包在wildfly上部署时,我没有问题,但在尝试测试服务时,我得到以下错误.
我不知道如何解决这个问题.
有人能帮帮我吗?
21:54:17,066 ERROR [io.undertow.request] (default task-19) UT005023: Exception handling request to /cdi-test/hello: org.jboss.resteasy.spi.UnhandledException: org.jboss.weld.exceptions.IllegalArgumentException: WELD-001456: Argument resolvedBean must not be null
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.8.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final] …Run Code Online (Sandbox Code Playgroud) 我尝试通过GET将参数传递给REST方法.
@GET
@Path("{id}")
public Response getUser(@PathParam("id") String id) {
Query qry = em.createQuery("from User c WHERE id = :user_id");
qry.setParameter("user_id", id);
List<User> results = qry.getResultList();
if(results.size() > 0) {
return Response.ok(results.get(0),MediaType.APPLICATION_JSON_TYPE).build();
} else {
return Response.serverError().status(Response.Status.NOT_FOUND).build();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我通过Rest Client调用它:
client = ClientBuilder.newClient();
Response response = client.target(TestPortProvider.generateURL("/user")+"/abc").request().get(Response.class);
Run Code Online (Sandbox Code Playgroud)
然后调用该方法,但参数为空.如果我"abc"从GET url中删除该方法,则不会调用该方法.此外,如果我删除@Path("{id}")它也不起作用.似乎有一个参数,但它没有任何理由是空的.也许有人可以帮我解决问题.
亲切的问候
我已经创建了一个jax-rs rest api并且放心地进行了测试.所有测试都是绿色的.现在我正在尝试为它创建一个html/js前端.
我的问题是我不知道我的json对象应该如何被我的其他api接受.感谢restassured/jax-rs我从未处理过请求字符串.我填写对象,我得到对象,(联合国)编组(json)是不可见的.
有什么方法可以看到(调试)由rest-assured/java创建的字符串并通过"wire"发送?
我已经映射了一个自定义解串器,以将dd / MM / yyyy模式下的字符串转换为LocalDate,以便可以使用更具可读性的签名来调用我的服务。
这是我的dto类,用作Jersey @BeanParam在层之间传输数据:
public class ProdutoFilterDto implements FilterDto {
private static final long serialVersionUID = -4998167328470565406L;
@QueryParam("dataInicial")
@JsonDeserialize(using = CustomLocalDateDeserializer.class)
private LocalDate dataInicial;
@QueryParam("dataInicial")
@JsonDeserialize(using = CustomLocalDateDeserializer.class)
private LocalDate dataFinal;
public LocalDate getDataInicial() {
return dataInicial;
}
public void setDataInicial(LocalDate dataInicial) {
this.dataInicial = dataInicial;
}
public LocalDate getDataFinal() {
return dataFinal;
}
public void setDataFinal(LocalDate dataFinal) {
this.dataFinal = dataFinal;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义反序列化器:
public class CustomLocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext …Run Code Online (Sandbox Code Playgroud)