Java 球衣 ExceptionMapper 中的 toResponse 不会被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22413239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
toResponse in jersey ExceptionMapper does not get invoked
提问by alex
So I'm building a web application, we are using JPA and Jersey to consume/produces JSON data.
所以我正在构建一个 Web 应用程序,我们使用 JPA 和 Jersey 来使用/生成 JSON 数据。
I have a custom "EntityException" aswell as a custom "EntityExceptionMapper"
我有一个自定义的“EntityException”以及一个自定义的“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:
我从 REST 调用中调用它:
@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:
但是我的响应是空的,我没有从我的 toResponse 方法中获取 sys。这与球衣网站上的示例非常相似:
https://jersey.java.net/nonav/documentation/1.12/jax-rs.html#d4e435
https://jersey.java.net/nonav/documentation/1.12/jax-rs.html#d4e435
What am I missing??
我错过了什么??
回答by Pierre-Luc Pineault
I had a similar problem where the ExceptionMapper
had the proper @Provider
annotation and the rest of the code was identical to Jersey's example but still wasn't registered properly.
我有一个类似的问题,其中ExceptionMapper
有正确的@Provider
注释,其余代码与 Jersey 的示例相同,但仍未正确注册。
Well it turns out I had to register manually my custom ExceptionMapper
within my HttpServlet
with the method addExceptionMapper
. Because it's now manually registered, the @Provider
annotation can be safely removed.
好吧,事实证明我必须使用方法ExceptionMapper
在 my 中手动注册我的自定义。因为它现在是手动注册的,所以可以安全地删除注释。HttpServlet
addExceptionMapper
@Provider
So with the following ExceptionMapper (I'm catching every RuntimeException
to rethrow them as 400)
因此,使用以下 ExceptionMapper (我正在捕获每个RuntimeException
以将它们重新抛出为 400)
public class MyCustomExceptionHandler implements ExceptionMapper<RuntimeException> {
@Override
public Response toResponse(RuntimeException exception) {
return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
}
}
I had to add the 2nd line in my init :
我必须在我的 init 中添加第二行:
HttpServlet serviceServlet = jerseyServletFactory.create(someResource);
jerseyServletFactory.addExceptionMapper(new MyCustomExceptionHandler()); //<--
httpServer.register(serviceServlet, "/api");
httpServer.start();
回答by mSolujic
I used spring to wire up jersey app and used @Component with @Provider.
我使用 spring 连接球衣应用程序并使用 @Component 和 @Provider。
When I moved to jersey v > 2.5, it stopped working.
当我换到球衣 v > 2.5 时,它就停止工作了。
I resolved this very issue by putting @Singleton annotation instead of @Component alongside @Provider, like this:
我通过在@Provider 旁边放置@Singleton 注释而不是@Component 来解决这个问题,如下所示:
@Provider
@Singleton
public class EntityExceptionMapper implements ExceptionMapper<EntityException> {...
回答by bluetech
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;
}
}
回答by brianmearns
I'm using the Jersey JdkHttpServerFactory
, and I just had to add the ExceptionMapper
class as a resource, just like my other controller resources:
我正在使用 Jersey JdkHttpServerFactory
,我只需将该ExceptionMapper
类添加为资源,就像我的其他控制器资源一样:
import com.sun.net.httpserver.HttpServer;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
// ...
Set<Class> resources = new HashSet<>();
// Add whatever other resource classes you have...
//--->>> Add the exception mapper <<<---
resources.add(EntityExceptionMapper.class);
ResourceConfig resources = new ResourceConfig(resources);
URI uri = UriBuilder.fromUri("http://localhost/").build();
HttpServer server = JdkHttpServerFactory.createHttpServer(uri, resources);
回答by Kalpesh Soni
I am still using jersey 1.17 , spring and jersy-spring
我还在用 jersey 1.17 , spring 和 jersy-spring
@Component annotation fixes this
@Component 注释解决了这个问题
回答by Ross H Mills III
I had the same problem and was able to fix it by including the package of my ExceptionMapper in the jersey.config.server.provider.packages in my web.xml file. Below is a snippet from my web.xml.
我遇到了同样的问题,并且能够通过在 web.xml 文件的 jersey.config.server.provider.packages 中包含我的 ExceptionMapper 包来修复它。下面是我的 web.xml 中的一个片段。
<servlet>
<servlet-name>voteride-servlet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.voteride.ws;com.voteride.errorHandling;org.codehaus.Hymanson.jaxrs
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.scanning.recursive</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
回答by SM ANSARI
I had the same problem. I just had to modify the web.xml.
Previously in my web.xml file param-value was com.two95.restful.resource
I just changed to root package com.two95.restful
. Then it started working like a charm with just the @Provider
annotation.
我有同样的问题。我只需要修改 web.xml。以前在我的 web.xml 文件 param-value 中com.two95.restful.resource
我只是更改为 root package com.two95.restful
。然后它开始像一个只有@Provider
注释的魅力一样工作。
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.two95.restful</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
回答by Dharmendra Kumar Singh
I also face the same issue.Just add the package name that have the ExceptionMappperHandler classes.
我也面临同样的问题。只需添加具有 ExceptionMappperHandler 类的包名称。
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>Service,Utilities.ExceptionMapper</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Here,service contain all service classes and Utilities.ExceptionMapper contains all exceptionMapper. Hope its help
在这里,service 包含所有服务类,Utilities.ExceptionMapper 包含所有 exceptionMapper。希望它的帮助
回答by avinash
Try to register your exception mapper class in your X extends ResourceConfigfile. register(CustomExceptionMapper.class);this line will help application to find your mapper class and return whatever you have written inside the toResponse method of mapper class
尝试在X extends ResourceConfig文件中注册您的异常映射器类。 注册(CustomExceptionMapper.class);这一行将帮助应用程序找到您的映射器类并返回您在映射器类的 toResponse 方法中编写的任何内容
回答by Kushwaha
I have encountered the same issue while develop sample REST API. While creating REST API i have given base package name like org.manish.rest.message, I supposed to create every other packages under the base package like this
我在开发示例 REST API 时遇到了同样的问题。在创建 REST API 时,我给了基本包名称,如 org.manish.rest.message,我应该像这样在基本包下创建所有其他包
- model -
org.manish.rest.message.model
- database -
org.manish.rest.message.database
- resource -
org.manish.rest.message.resource
- 模型 -
org.manish.rest.message.model
- 数据库 -
org.manish.rest.message.database
- 资源——
org.manish.rest.message.resource
in web.xml init param was given like this
在 web.xml init 参数中是这样给出的
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.manish.rest.message</param-value>
</init-param>
It means, i have registered my base package in web.xml, what ever package i will create under this; will be consider by JAX-RS based on my call and requirement. But when i created my exception package by mistake i put package name org.manish.rest.exception. Since this was not registered in web.xml so my complete exception class was not considered to handle exception by JAX-RS.
As a correction, i have just modified my exception package name from
org.manish.rest.exception
to org.manish.rest.message.exception
这意味着,我已经在 web.xml 中注册了我的基本包,我将在此下创建什么包;JAX-RS 将根据我的电话和要求考虑。但是当我错误地创建了我的异常包时,我把包名 org.manish.rest.exception。由于这未在 web.xml 中注册,因此我的完整异常类不被 JAX-RS 视为处理异常。作为更正,我刚刚将异常包名称从 修改
org.manish.rest.exception
为 org.manish.rest.message.exception
After that i executed once in post man and i got expected result.
之后我在邮递员中执行了一次,我得到了预期的结果。
Hope this can solve your query.
希望这可以解决您的疑问。
Thanks Manish
谢谢曼尼什