Jersey ExceptionMapper未被调用 [英] Jersey ExceptionMapper not being invoked
问题描述
我正在尝试调用此异常映射器以返回404未找到的响应,但它一直返回500内部错误。泽西版是2.22.1。下面的代码片段。感谢所有帮助。
I am trying to invoke this exception mapper to return a 404 not found response but it keeps returning 500 internal error. Jersey version is 2.22.1. Code snippet below. Appreciate all help.
谢谢。
异常映射器类。
package org.learn.rest.messengerdemo.exception;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>{
@Override
public Response toResponse(DataNotFoundException ex) {
return Response.status(Response.Status.FORBIDDEN).build();
}
}
异常类。
package org.learn.rest.messengerdemo.exception;
package org.learn.rest.messengerdemo.exception;
public class DataNotFoundException extends RuntimeException{
private static final long serialVersionUID = 2176642539344388961L;
public DataNotFoundException(String message)
{
super(message);
}
}
服务类抛出的方法。
public Message getMessage(long messageId) {
Message message = messages.get(messageId);
if(message == null)
{
throw new DataNotFoundException("Message with id " + messageId + " not found");
}
return message;
}
和资源类。
@GET
@Path("/{messageId}")
public Message getMessage(@PathParam("messageId") long messageId) {
return messageService.getMessage(messageId);
}
推荐答案
查看<$ c来自您的之前的问题的$ c> web.xml ,您有这个
Looking at the web.xml
from your previous question, you have this
<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>org.learn.rest.messengerdemo.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这个init-parm是什么 jersey.config.server.provider.packages
说泽西岛应该扫描命名包中的 @Path
带注释的资源类和 @Provider
注释提供者类并注册它们。
What this init-parm jersey.config.server.provider.packages
says is that Jersey should scan the named package for @Path
annotated resource class and @Provider
annotated provider classes and register them.
您只有资源包 org.learn.rest.messengerdemo.resources
列出,但您 ExceptionMapper
位于不同的包中。默认行为是递归扫描,也表示子包。因此,如果您列出 org.learn.rest.messengerdemo
,则会同时命中资源包和例外包。或者您可以列出两个包,用逗号或分号分隔。无论哪种方式都可行
You only have the resources package org.learn.rest.messengerdemo.resources
listed, but you ExceptionMapper
is in a different package. The default behavior is to scan recursively, meaning sub-packages also. So if you listed org.learn.rest.messengerdemo
instead, you would hit both the resources package and the exceptions package. Or you could list both packages, separated by comma or semi-colon. Either way would work
<param-value>org.learn.rest.messengerdemo</param-value>
<!-- OR -->
<param-value>
org.learn.rest.messengerdemo.resources,
org.learn.rest.messengerdemo.exception
</param-value>
这篇关于Jersey ExceptionMapper未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!