当我发送一个对象而不是一个数组时,我得到了ProcessingException。即使使用javax.ws.rs.ext.ExceptionMapper ,我也无法捕获ProcessingException
我正在使用球衣2.25
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response testInJSON(Test[] test) {
String result = "Test message : " + test[0].getTest();
return Response.status(Response.Status.ACCEPTED).entity(result).build();
}
POM文件
<properties>
<jersey2.version>2.25</jersey2.version>
<jaxrs.version>2.0.1</jaxrs.version>
<tomcat.version>7.0.69</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${tomcat.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
堆栈跟踪:
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:308)
Test.java
public class Test implements Serializable{
private static final long serialVersionUID = 1L;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
更新:
这似乎是一个框架错误。吉拉(Jira)已打开。 https://java.net/jira/browse/JERSEY-3222
最佳答案
ProcessingException处理由ResponseErrorMapper覆盖,而不是ExceptionMapper:
public class MyExceptionMapper implements ExceptionMapper<Throwable>, ResponseErrorMapper {
...
为了使其正常工作,您需要在应用程序配置中设置“ PROCESSING_RESPONSE_ERRORS_ENABLED”属性:
val config = ResourceConfig()
config.register(new MyExceptionMapper(...))
config.setProperties(new LinkedHashMap<String, Object>() {{
put(org.glassfish.jersey.server.ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true);
}});