在运动衫中的响应ExceptionMapper不被调用 [英] toResponse in jersey ExceptionMapper does not get invoked
问题描述
所以我正在构建一个Web应用程序,我们使用JPA和Jersey来消费/生成JSON数据。
So I'm building a web application, we are using JPA and Jersey to consume/produces JSON data.
我有一个自定义的EntityException以及一个自定义EntityExceptionMapper
I have a custom "EntityException" aswell as a custom "EntityExceptionMapper"
这是映射器:
@Provider
public class EntityExceptionMapper implements ExceptionMapper<EntityException> {
public EntityExceptionMapper() {
System.out.println("Mapper created");
}
@Override
public Response toResponse(EntityException e) {
System.out.println("This doesnt print!");
return Response.serverError().build();
}
}
我的例外:
public class EntityException extends Exception implements Serializable{
public EntityException(String message) {
super(message);
System.out.println("This prints...");
}
}
我打电话给一个REST调用:
And I'm calling it from a REST call:
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String test() throws EntityException{
throw new EntityException("This needs to be send as response!!");
//return "test";
}
我的问题是,当抛出上述异常时,我进入构造函数(打印:打印...)编辑:我还得到:Mapper created!
My problem is that, when the above exception is thrown, I get in the constructor (prints: "This prints...") I also get the: "Mapper created!"
但是我的回复是空的,我不得到我的toResponse方法的sys。这与球衣网站上的例子非常相似:
But my response is empty, and I don't get to the sys out of my toResponse method. This is really similar to the example on the jersey website:
https://jersey.java.net/nonav/documentation/1.12/jax-rs.html#d4e435
我缺少什么?
推荐答案
我正在使用部署不可知应用程序模型,以便以下功能适用于我: / p>
I am using deployment agnostic application model so the following worked for me:
public class MyApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloWorldResource.class);
/** you need to add ExceptionMapper class as well **/
s.add(EntityExceptionMapper.class)
return s;
}
}
这篇关于在运动衫中的响应ExceptionMapper不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!