作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 JAX-RS jersey ExceptionMapper,我想知道是否有办法在方法 toResponse() 内部知道哪个方法(来自 API)抛出了异常。
示例(伪代码)
@javax.ws.rs.Path(“/bookstore/books/{bookID}”)
public class Book {
@javax.ws.rs.GET
public String informationMethod(String user) {
...
throw new Exception("Error");
....
}
}
@Provider
public class SomeMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception ex) {
//a way to get the method name that threw the exception.
//In the above example is: informationMethod.
String methodName = //informationMethod
return Response.status(500).entity(ex.getMessage()).type("text/plain")
.build();
}
}
最佳答案
您可以从上下文中向您的 ExceptionMapper
提供一些信息。
package your.rest.pckg;
import java.lang.reflect.Method;
import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class SomeMapper
implements ExceptionMapper<Exception>
{
@Context private HttpServletRequest request;
@Context private HttpServletResponse response;
@Context private ResourceInfo resourceInfo;
@Context private UriInfo uriInfo;
@Override
public Response toResponse( Exception ex )
{
String method = request.getMethod();
String pathInfo = request.getPathInfo();
Class<?> resourceClass = resourceInfo.getResourceClass();
Method resourceMethod = resourceInfo.getResourceMethod();
URI resourcePath = getResourcePath( resourceClass, resourceMethod );
URI requestUri = uriInfo.getRequestUri();
MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
// define your object to provide data through response
Object responseEntity = ex.getMessage();
// do your stuff
return Response.status( Response.Status.INTERNAL_SERVER_ERROR )
.entity( responseEntity )
.type( MediaType.APPLICATION_JSON )
.build();
}
private static URI getResourcePath( Class<?> clazz, Method method )
{
if ( clazz == null || !clazz.isAnnotationPresent( Path.class ) )
{
return null;
}
UriBuilder builder = UriBuilder.fromResource( clazz );
if ( method != null && method.isAnnotationPresent( Path.class ) )
{
builder.path( method );
}
return builder.build();
}
}
见
除了 Excpetion
,您还可以映射 Throwable
。
要通过 WebApplicationExcpetion
,只需在 toResponse()
主体的开头添加以下 if 子句:
if (ex instanceof WebApplicationException)
{
return (( (WebApplicationException) ex ).getResponse());
}
您还可以在资源类 Book
中使用所有 @Context
字段。
关于java - JAX-RS Jersey ExceptionMapper : How to know the method who threw the exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41301386/
我是一名优秀的程序员,十分优秀!