[英]Resteasy does not find ExceptionMapper provider
I have tried a lot of things to make this work, but it is not working anyway. 我已经尝试了很多东西来完成这项工作,但它无论如何都没有用。 Also, I don't find proper documentation about this.
另外,我没有找到关于此的适当文档。 I am trying to implement a
CustomExceptionMapper
for my CustomException
type. 我试图为我的
CustomException
类型实现一个CustomExceptionMapper
。 The CustomException
is thrown correctly, but it is not catched. 正确抛出
CustomException
,但它没有被CustomException
。
I thought that annotating the CustomExceptionMapper
with @Provider
was enough, but it isnot detected. 我认为,诠释着
CustomExceptionMapper
与@Provider
已经足够了,但状态并没有检测到。 I have tried to allow scanning in web.xml, but I've found this: https://issues.jboss.org/browse/AS7-1739 . 我试图允许在web.xml中扫描,但我发现了这个: https : //issues.jboss.org/browse/AS7-1739 。 I am using 7.0.1 Final.
我正在使用7.0.1 Final。 Probably the solution is changing of version, but this decision is not up to me.
可能解决方案是改变版本,但这个决定不适合我。
I have also found that you can try to override the getClasses()
or getSingletons
method and add there your CustomExceptionMapper
, but it is just not detecting it. 我还发现你可以尝试覆盖
getClasses()
或getSingletons
方法并在那里添加你的CustomExceptionMapper
,但它只是没有检测到它。
How my Application class looks like: 我的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;
}
}
And the Mapper 和Mapper
@Provider
public class TestExceptionMapper implements ExceptionMapper<TestException> {
public Response toResponse(TestException ex) {
//something, but I cannot reach it.
}
}
Web.xml 在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>
And the service which is being called through URL: 通过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 {
// ....
}
If you call this with the correct parameters, it works. 如果用正确的参数调用它,它就可以工作。 But if you put a bad parameter, it does not.
但是如果你输入一个错误的参数,它就不会。
I would like to say too that the application is working, but I just wanted to add the Mappers. 我也想说应用程序正在运行,但我只是想添加Mappers。
does your web.xml have the context-param for resteasy providers? 您的web.xml是否具有resteasy提供程序的context-param?
something like this 这样的事情
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>packagename.TestExceptionMapper</param-value>
</context-param>
Exception mappers works for the exceptions thrown from the body of your resource method. 异常处理程序适用于从资源方法主体抛出的异常。 If you modify your resource to throw a
TestException
: 如果您修改资源以抛出
TestException
:
public String updateSometh(@NotNull @QueryParam("key") String key,
@QueryParam(value = "somethID") Long somethID) throws TestException {
throw new TestException();
}
the mapper should catch the exception. 映射器应该捕获异常。 The problem is in the way you tested.
问题出在你测试的方式上。
You can also register a custom ExceptionMapper in your javax.ws.rs.core.Application class by overriding the classes() method like so: 您还可以通过重写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;
}
}
The ExceptionMapper itself can use java.lang.Exception as Type parameter to handle any kind of exception. ExceptionMapper本身可以使用java.lang.Exception作为Type参数来处理任何类型的异常。
@Provider
public class CustomExceptionMapper implements ExceptionMapper<Exception> {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.