泽西岛 2 POST 且 PUT 参数为空
Jersey 2 POST and PUT parameter is null
当方法为 PUT 或 POST 时,我的球衣休息服务有问题。问题是参数为空。我用 angularjs 和 jersey-test-framework 测试结果相同,参数为空。
我认为问题是由 header "Content-Type" 引起的,但我在测试中添加了它,结果相同。
球衣版本:2.14
球衣模块: jersey-container-servlet, jersey-spring3, jersey-media-json-jackson
我的应用程序配置和 object 映射器:
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");
}
问题出在 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");
}
}
当方法为 PUT 或 POST 时,我的球衣休息服务有问题。问题是参数为空。我用 angularjs 和 jersey-test-framework 测试结果相同,参数为空。
我认为问题是由 header "Content-Type" 引起的,但我在测试中添加了它,结果相同。
球衣版本:2.14
球衣模块: jersey-container-servlet, jersey-spring3, jersey-media-json-jackson
我的应用程序配置和 object 映射器:
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");
}
问题出在 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");
}
}