JAX-RS球衣ExceptionMapper:如何知道引发异常的方法 [英] JAX-RS jersey ExceptionMapper: How to know the method who threw the exception

查看:83
本文介绍了JAX-RS球衣ExceptionMapper:如何知道引发异常的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-RS球衣ExceptionMapper,我想知道是否有一种方法可以知道toResponse()方法内部,哪个方法(来自API)引发了异常.

Im using JAX-RS jersey ExceptionMapper, and I'm wondering if there is a way to know inside the method toResponse(), which method (from the API) threw the exception.

示例(虚拟代码)

@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();
    }
}

推荐答案

您可以从上下文向ExcpetionMapper提供一些信息.

You can provide some information from context to your ExcpetionMapper.

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();
    }
}

请参见

  • HttpServletRequest.java
  • HttpServletResponse.java
  • ResourceInfo.java
  • UriInfo.java

您也可以映射Throwable,而不是Excpetion.

Instead of Excpetion you can also map Throwable.

要通过WebApplicationExcpetion,只需在toResponse()正文的beginnig处添加以下if子句即可:

To pass through WebApplicationExcpetions just add following if clause at the beginnig of toResponse() body:

if (ex instanceof WebApplicationException)
{
    return (( (WebApplicationException) ex ).getResponse());
}


您还可以使用资源类Book中的所有@Context字段.


You can also use all of the @Context fields in your resource class Book.

这篇关于JAX-RS球衣ExceptionMapper:如何知道引发异常的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆