春季如何从WebRequest获取请求的URI?

时间:2018-08-19 20:34:27

标签: spring spring-mvc spring-rest

我正在Spring Rest Web服务中使用@ControllerAdviceResponseEntityExceptionHandler处理REST异常。到目前为止,一切工作正常,直到我决定将URI路径(发生异常的路径)添加到 BAD_REQUEST 响应中。

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    logger.info(request.toString());
    return handleExceptionInternal(ex, errorMessage(HttpStatus.BAD_REQUEST, ex, request), headers, HttpStatus.BAD_REQUEST, request);
}

private ApiError errorMessage(HttpStatus httpStatus, Exception ex, WebRequest request) {
    final String message = ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage();
    final String developerMessage = ex.getCause() == null ? ex.toString() : ex.getCause().getMessage();
    return new ApiError(httpStatus.value(), message, developerMessage, System.currentTimeMillis(), request.getDescription(false));
}

ApiError只是Pojo类:

public class ApiError {

    private Long timeStamp;
    private int status;
    private String message;
    private String developerMessage;
    private String path;
}

但是WebRequest没有提供任何api来获取请求失败的路径。我试过了: request.toString()返回-> ServletWebRequest:uri = / signup; client = 0:0:0:0:0:0:0:1
request.getDescription(false)返回-> uri = / signup
getDescription接近要求,但不满足要求。有没有办法只获得uri部分?

5 个答案:

答案 0 :(得分:6)

找到了解决方案。将WebRequest强制转换为ServletWebRequest可以解决这个问题。

((ServletWebRequest)request).getRequest().getRequestURL().toString()

返回完整路径-http://localhost:8080/signup

答案 1 :(得分:2)

对此问题有多种解决方案。

1)可以使用以下命令从WebRequest获取请求URI和客户端信息 webRequest.getDescription(true)。

true将显示用户信息,例如客户端ID,false将仅显示URI。

2)代替直接在方法定义中使用HttpServletRequest的WebRequest作为

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request, HttpServletRequest httpRequest) {
    logger.info(httpRequest.getRequestURI());
    return handleExceptionInternal(ex, errorMessage(HttpStatus.BAD_REQUEST, ex, request), headers, HttpStatus.BAD_REQUEST, request);
}

答案 2 :(得分:1)

访问WebRequest对象的属性:

Object obj = webRequest.getAttribute("org.springframework.web.util.UrlPathHelper.PATH", 0)
String uri = String.valueOf(obj);
webRequest.getAttribute(String attributeName, int scope);

// scope can be either:
//   0: request
//   1: session

// valid attribute names can be fetched with call:
String[] attributeNames = webRequest.getAttributeNames(0);   //scope is request

有效的属性名称是:

org.springframework.web.util.UrlPathHelper.PATH
org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER
org.springframework.web.servlet.HandlerMapping.bestMatchingHandler
org.springframework.web.servlet.DispatcherServlet.CONTEXT
org.springframework.web.servlet.resource.ResourceUrlProvider
characterEncodingFilter.FILTERED
org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR
org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE
org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER
formContentFilter.FILTERED
org.springframework.web.servlet.HandlerMapping.bestMatchingPattern
requestContextFilter.FILTERED
org.springframework.web.servlet.DispatcherServlet.OUTPUT_FLASH_MAP
org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping
org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER
org.springframework.web.servlet.HandlerMapping.uriTemplateVariables
org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER
org.springframework.core.convert.ConversionService

答案 3 :(得分:0)

ResponseEntityExceptionHandler 说明 @ControllerAdvice类的便捷基类,这些类希望通过@ExceptionHandler方法在所有@RequestMapping方法中提供集中式异常处理。 here < / p>

在Spring Boot 2.1.6中,您可以编写如下内容:

RestExceptionHandler.java

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
 
private static final Logger logger = LoggerFactory.getLogger(RestExceptionHandler.class);

@ExceptionHandler(ResourceNotFoundException.class)
protected ResponseEntity<Object> handleEntityNotFound(ResourceNotFoundException ex, final HttpServletRequest httpServletRequest) {
    ApiError apiError = new ApiError(HttpStatus.NOT_FOUND);
    apiError.setMessage("Resource not found");
    apiError.setDebugMessage(ex.getMessage());
    apiError.setPath(httpServletRequest.getRequestURI());
    return buildResponseEntity(apiError);
}

private ResponseEntity<Object> buildResponseEntity(ApiError apiError) {
    return new ResponseEntity<>(apiError, apiError.getStatus());
}


 
 @Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED);
        apiError.setMessage(ex.getMessage());
        apiError.setPath(((ServletWebRequest)request).getRequest().getRequestURI().toString());
        logger.warn(ex.getMessage());
        return buildResponseEntity(apiError);
}
}

让我们从实现一个用于发送错误的简单结构开始:

ApiError.java

public class ApiError {
// 4xx and 5xx
private HttpStatus status;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private LocalDateTime timestamp;

// holds a user-friendly message about the error.
private String message;

// holds a system message describing the error in more detail.
@JsonInclude(value = Include.NON_EMPTY)
private String debugMessage;

// returns the part of this request's URL
private String path;

@JsonInclude(value = Include.NON_EMPTY)
private List<String> details=new ArrayList<>();

// setters & getters
}

ResourceNotFoundException.java

public class ResourceNotFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;

public ResourceNotFoundException() {
    super();
}

public ResourceNotFoundException(String msg) {
    super(msg);
}

答案 4 :(得分:0)

您可以使用 request.getDescription(false)。