Tag: jersey

如何在请求filter中获取表单参数

我正在尝试在请求filter中获取请求的表单参数: @Override public ContainerRequest filter(final ContainerRequest request) { final Form formParameters = request.getFormParameters(); //logic return request; } 但是,表格似乎总是空的。 HttpRequestContext.getFormParameters()文档说: 获取请求实体的表单参数。 此方法将确保缓冲请求实体,以便应用程序可以使用它。 返回:表单参数,如果有请求实体且内容类型为“application / x-www-form-urlencoded”,否则将返回不包含参数的实例。 我的资源是用@Consumes(“application/x-www-form-urlencoded”)注释的,虽然它在请求filter之后才会匹配 – 这就是为什么这不起作用? 我尝试做一些研究,但未能找到任何确凿的证据certificate这是否可行。 有这个为期4年的讨论 ,Paul Sandoz说: 如果您正在使用Jerseyfilter或使用HttpRequestContext您可以获得如下表单参数:[到Jersey 1.1.1的断开链接HttpRequestContext.getFormParameters ] 我还发现了这个有关如何在请求filter中获取multipart / form-data表单字段的3年讨论 。 其中,Paul Sandoz使用以下代码: // Buffer InputStream in = request.getEntityInputStream(); if (in.getClass() != ByteArrayInputStream.class) { // Buffer input ByteArrayOutputStream […]

用实体抛出的WebApplicationExceptions的ExceptionMapper?

在我们的(遗留)代码库中,我们以不同的方式抛出WebApplicationExceptions。 为了对我们如何处理exception做出一些命令 – 我想为这些WAE(和其他)创建一个ExceptionMapper。 然而,我意识到Jersey的ExceptionMapper只映射了没有被实体抛出的WAE。 例如: throw new WebApplicationException(Response.status(500).build()); ExceptionMapper捕获此exception。 throw new WebApplicationException(Response.status(500).entity(WsResourceUtils.createWSResourceRestError(500, “bla”)).build()); ExceptionMapper不会捕获此exception。 两者都是从代码中的相同点抛出的。 这是我的ExceptionMapper: @Provider public class GeneralExceptionMapper implements ExceptionMapper { private static final Logger logger = LoggerFactory.getLogger(GeneralExceptionMapper.class); @Override public Response toResponse(Throwable e) { logger.error(“Caught a WAE”, e); … } 是否有可能创建一个ExceptionMapper,即使它们的响应已经使用实体构建,它也会捕获WebApplicationExceptions? 我们正在使用Jersey 1.17。 谢谢。

泽西/jackson@JsonIgnore在二传手

我有一个带有以下注释的类: class A { public Map<String,List> references; @JsonProperty public Map<String,List> getReferences() { … } @JsonIgnore public void setReferences(Map<String,List>) { } … } } 我尝试的是忽略反序列化的json。 但它不起作用。 始终当JSON String到达时,Jackson lib会填充references属性。 如果我只使用@JsonIgnore注释,则getter不起作用。 这个问题有什么解决方案吗? 谢谢

当jackson反序列化失败时,Jerseyexception映射器无法正常工作

我在我的REST API中使用Jersey 2.10和Jackson序列化/反序列化function。 我的想法是让我的REST API始终返回标准的JSON错误响应。 为此,我有ExceptionMapper类,为Jersey应用程序中抛出的任何exception构建正确的json错误响应。 我还有一个生成相同类型的JSON响应的jsp,我在web.xml中注册为error-page,它涵盖了Jersey加载之前可能出现的所有错误。 但是有一种情况,我的Exception mappers和我的json生成jsp都没有工作,就是当一个坏的形成的json发送到POST REST端点时,它只返回以下消息: HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, DELETE, PUT Content-Type: text/plain Content-Length: 210 Date: Tue, 24 Jun 2014 22:14:11 GMT Connection: close Can not deserialize instance of com.example.rest.User[] out of START_OBJECT token at [Source: org.glassfish.jersey.message.internal.EntityInputStream@1dcccac; line: 1, column: 1] 如何让Jersey返回我的自定义错误响应而不是这个? 更新: […]

使用swagger或任何其他工具生成Rest API文档

我正在寻找一种方法来记录我的Rest API。 我的服务器是Tomcat / Spring服务器,Rest API是使用Jenkins实现的。 Swagger似乎是一个非常酷的解决方案,但我无法弄清楚我如何使用它与我的代码。 我正在寻找创建json swagger-ui可以读取的最好方法 – 我该怎么做? 此外,我很乐意检查在此类环境中记录Rest API的任何其他好的解决方案。

泽西(JSR311-实施)和重定向

有没有办法在Jersey资源中重定向用户代理? 在Spring MVC中有“redirect:” – 语法,但我在Jersey的Viewable类中找不到任何可比性。 我发现工作的唯一方法是使用HttpServletResponse.sendRedirect() 。

JUL适配器不适用于Jersey

我正在尝试使用JUL Adapter将Java Util Logging委派给Log4j2。 更准确地说,任何使用JUL生成日志的第三方库都应该委托给Log4j2。 作为一个简单的练习,我创建了一个使用库的独立应用程序(我创建此库用于测试目的,它使用JUL生成日志)来测试JUL适配器 。 当我按照此处所述更改日志管理器时,我可以看到效果。 它工作正常。 她是代码: import org.apache.logging.log4j.LogManager; import com.ah.loggen.LogGenerator; public class TestLogging { static { System.setProperty(“java.util.logging.manager”, “org.apache.logging.log4j.jul.LogManager”); } private static final org.apache.logging.log4j.Logger LOG4J = LogManager.getLogger(); public static void main(String[] args) { System.out.println(“Java Util Logging”); LogGenerator.generateError(“This is an error message.”); LogGenerator.generateInfo(“This is an info message.”); LogGenerator.generateWarning(“This is a warning message.”); System.out.println(“LOG4J”); […]

如何在Jersey ContainerResponseFilter中获取资源注释

Jersey提供两个类来与资源上的注释进行交互: ResourceFilterFactory ,一个类可以inheritance它,以便在应用程序启动时触发一次 ContainerRequestFilter , ContainerResponseFilter ,几个类可以inheritance它们以在每个请求/响应上触发它们 ResourceFilterFactory定义了一个create方法(实现),它采用AbstractMethod来提供对方法和类注释的访问。 ContainerRequestFilter和ContainerResponseFilter定义了一个filter方法(实现),它接受请求/响应,但那些只能访问被调用的方法注释,而不是第一类。 我正在尝试实现一个CacheControl注释,它以下列方式定义HTTP缓存头。 @Path(“/path”) @CacheControl(maxAge = 172800) public class Resource { @GET @Path(“/{id}”) @CacheControl(mustRevalidate = true) public Response get(@PathParam(“id”) Long id) { … } } 我的问题是我不想为我的应用程序中定义的每个REST方法创建一个新的CacheControlFilter 。 public class FilterFactory implements ResourceFilterFactory { @Override public List create(AbstractMethod method) { List filters = newArrayList(); if (isAnnotationPresent(method, CacheControl.class)) filters.add(new CacheControlFilter(method)); […]

如何在Jersey中获取上传文件的MIME类型

我在泽西岛有一个标准的上传端点: @POST @Secure @Consumes(“multipart/form-data”) public Response upload( @Context final HttpHeaders hh, @FormDataParam(“fileaaa”) final FormDataContentDisposition disposition, @FormDataParam(“fileaaa”) final InputStream input, 如何获取上传文件的MIME类型? 如果我执行disposition.getType这将获取表单的MIME类型; 在这种情况下, form-data 。 我知道信息在某处; HTTP消息应该是这样的: —————————–7d01ecf406a6 Content-Disposition: form-data; name=”input_text” mytext —————————–7d01ecf406a6 Content-Disposition: form-data; name=”fileaaa”; filename=”C:\Inetpub\wwwroot\Upload\pic.gif” Content-Type: image/gif (binary content) —————————–7d01ecf406a6–

Marshal / Un marshal使用JAXB在Jersey JAX-RS中列出对象

早上好。 今天早上,当我通过Jersey Entity提供者MessageBodyReader和MessageBodyWriter我遇到了以下问题。 我想编写一个资源方法和客户端,它返回一个自定义对象列表,媒体类型是application/xml 。 所以我想使用JAXB(我是JAXB的新手)。 我能够通过编写自己的扩展MessageBodyReader和MessageBodyWriter来实现这一目标。 但我害怕跟随的方式。 看看我实施的方式: 资源方法: @Path(“productlist/xml”) @GET public RetObjects getProductsXml(){ List pList = new ArrayList(); pList.add(new Product(“1″,”Dell latitude E6000”,2900,500)); pList.add(new Product(“2″,”Xperia Z2″,549,400)); RetObjects obj = new RetObjects(); obj.setObject(pList); return obj; } 我的自定义对象: @Entity @Table (name=”PRODUCT”) @XmlRootElement(name=”product”) public class Product { @Id @Column(name = “CODE”) private String code; … // rest […]