[英]Resteasy does not find ExceptionMapper provider
我已经尝试了很多东西来完成这项工作,但它无论如何都没有用。 另外,我没有找到关于此的适当文档。 我试图为我的CustomException
类型实现一个CustomExceptionMapper
。 正确抛出CustomException
,但它没有被CustomException
。
我认为,诠释着CustomExceptionMapper
与@Provider
已经足够了,但状态并没有检测到。 我试图允许在web.xml中扫描,但我发现了这个: https : //issues.jboss.org/browse/AS7-1739 。 我正在使用7.0.1 Final。 可能解决方案是改变版本,但这个决定不适合我。
我还发现你可以尝试覆盖getClasses()
或getSingletons
方法并在那里添加你的CustomExceptionMapper
,但它只是没有检测到它。
我的Application类如何:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/service")
public class MyApp extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(TestExceptionMapper.class);
return classes;
}
}
和Mapper
@Provider
public class TestExceptionMapper implements ExceptionMapper<TestException> {
public Response toResponse(TestException ex) {
//something, but I cannot reach it.
}
}
在web.xml
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.mycompany.rest.exceptions.TestExceptionMapper</param-value>
</context-param>
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>com.mycompany.rest.application.myApplication</param-value>
</init-param>
</servlet>
通过URL调用的服务:
@Stateless
@Path("/somePath")
public class someService {
@GET //this should be post, but for testing I'm using get
@Path("/some/update/someth")
@Produces()
public String updateSometh(@NotNull @QueryParam("key") String key, @QueryParam(value = "somethID") Long somethID) throws TestException {
// ....
}
如果用正确的参数调用它,它就可以工作。 但是如果你输入一个错误的参数,它就不会。
我也想说应用程序正在运行,但我只是想添加Mappers。
您的web.xml是否具有resteasy提供程序的context-param?
这样的事情
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>packagename.TestExceptionMapper</param-value>
</context-param>
异常处理程序适用于从资源方法主体抛出的异常。 如果您修改资源以抛出TestException
:
public String updateSometh(@NotNull @QueryParam("key") String key,
@QueryParam(value = "somethID") Long somethID) throws TestException {
throw new TestException();
}
映射器应该捕获异常。 问题出在你测试的方式上。
您还可以通过重写classes()方法在javax.ws.rs.core.Application类中注册自定义ExceptionMapper,如下所示:
@ApplicationPath("/api")
public class RestApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
// other resource classes ...
classes.add(CustomExceptionMapper.class);
return classes;
}
}
ExceptionMapper本身可以使用java.lang.Exception作为Type参数来处理任何类型的异常。
@Provider
public class CustomExceptionMapper implements ExceptionMapper<Exception> {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.