Jersey 2 POST和PUT参数为空

时间:2015-03-03 20:17:05

标签: java jax-rs jersey-2.0

当方法是PUT或POST时,我的球衣休息服务有问题。问题是参数为null。我用angularjs和jersey-test-framework测试结果相同,参数为null。

我认为问题是由于标题为“Content-Type”,但我在测试中添加了相同的结果。

泽西岛版:2.14

泽西模块:jersey-container-servlet,jersey-spring3,jersey-media-json-jackson

我的应用程序配置和对象映射器:

public class CustomObjectMapper implements ContextResolver<ObjectMapper> {

   final ObjectMapper defaultObjectMapper;

   public CustomObjectMapper() {
      defaultObjectMapper = createDefaultMapper();
   }

   @Override
   public ObjectMapper getContext(final Class<?> type) {
      return defaultObjectMapper;
   }

   private static ObjectMapper createDefaultMapper() {
      final ObjectMapper result = new ObjectMapper();

      return result;
   }

}

@ApplicationPath("/")
public class ApplicationConfig extends ResourceConfig {

   public ApplicationConfig() {
      packages("es.fjtorres.cpFacturas.server.api.impl",
            "es.fjtorres.cpFacturas.server.config",
            "es.fjtorres.cpFacturas.server.filter",
            "com.fasterxml.jackson.jaxrs");

      property(
           "jersey.config.beanValidation.enableOutputValidationErrorEntity.server",
            true);

      register(CustomObjectMapper.class);
      register(JacksonFeature.class);
   }
}

我的资源部分代码:

@Named
@Path(CUSTOMERS_PATH)
@Produces(MediaType.APPLICATION_JSON)
public class CustomerResourceImpl extends AbstractResource implements
      ICustomerResource 

   ...

   @Override
   @POST
   @Consumes(MediaType.APPLICATION_JSON)
   public Response add(final CustomerDto pDto) {
      try {
         getService().add(pDto);
      } catch (final ValidationException ve) {
         badRequest(ve.getErrors());
      }

      return Response.ok().build();
   }

}

我的单元测试:

@Test
public void addTest() {
    final Response response = target(NameTokens.CUSTOMERS_PATH).request()
            .header("Content-Type", MediaType.APPLICATION_JSON)
            .post(Entity.entity(new CustomerDto(), MediaType.APPLICATION_JSON));

    Assert.assertNotNull(response, "Response cannot be null");
    Assert.assertEquals(response.getStatus(), Status.OK.getStatusCode(),
            "Response status code isn't ok");
}

1 个答案:

答案 0 :(得分:0)

问题是CORS过滤器,我查看过滤器代码以解决POST和PUT请求的问题。我有两个CORS过滤器(一个用于请求,另一个用于响应),代码如下。

@Provider
public class CorsRequestFilter implements ContainerRequestFilter {

   @Override
   public void filter(final ContainerRequestContext requestContext)
         throws IOException {
      if (requestContext.getRequest().getMethod().equals(HttpMethod.OPTIONS)) {
         requestContext.abortWith(Response.status(Response.Status.OK).build());
      }
   }
}

@Provider
public class CorsResponseFilter implements ContainerResponseFilter {

    @Override
    public void filter(final ContainerRequestContext requestContext,
            final ContainerResponseContext responseContext) throws IOException {
        final MultivaluedMap<String, Object> headers = responseContext.getHeaders();

        headers.add("Access-Control-Allow-Origin", "*");
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
        headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Auth-Token");
    }

}