toResponse in jersey ExceptionMapper does not get invoked(球衣 ExceptionMapper 中的 toResponse 没有被调用)
问题描述
So I'm building a web application, we are using JPA and Jersey to consume/produces JSON data.
I have a custom "EntityException" aswell as a custom "EntityExceptionMapper"
Here's the mapper:
  @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();
    }
}
My Exception:
public class EntityException extends Exception implements Serializable{
  public EntityException(String message) {
      super(message);
      System.out.println("This prints...");
  }
}
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";
}
My problem is that, when the above exception is thrown, I get in the constructor (prints: "This prints...") Edit: I also get the: "Mapper created!"
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
What am I missing??
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 中的 toResponse 没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:球衣 ExceptionMapper 中的 toResponse 没有被调用
				
        
 
            
        - 如何使用WebFilter实现授权头检查 2022-01-01
 - C++ 和 Java 进程之间的共享内存 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 
						
						
						
						
						