如何在 HTTP 状态码后添加自定义消息 [英] How can I add custom message after HTTP status code

查看:24
本文介绍了如何在 HTTP 状态码后添加自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Boot 作为 Web 服务器,有时我想响应 [HTTP/1.1 403 XXX].

I'm using Spring Boot as a web server, and I want to response [HTTP/1.1 403 XXX] sometimes.

我用 @ResponseStatusresponse.sendError(403, "XXX") 尝试了 ResponseEntity,但我嗅探数据包发现网络服务器仅响应 [HTTP/1.1 403]

I tried ResponseEntity with @ResponseStatus, and response.sendError(403, "XXX"), but I sniffer packet found web server only response [HTTP/1.1 403]

卷曲示例:

[root@localhost ~]# curl -v 192.168.12.36:8080/test
* About to connect() to 192.168.12.36 port 8080 (#0) 
*   Trying 192.168.12.36...
* Connected to 192.168.12.36 (192.168.12.36) port 8080 (#0)
> GET /test HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.12.36:8080
> Accept: */*
> 
< HTTP/1.1 403 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 09 Aug 2018 08:26:37 GMT
< 
* Connection #0 to host 192.168.12.36 left intact

我需要的回应是:

< HTTP/1.1 403 XXX
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
...

如何在 HTTP 状态码后添加自定义消息?

How can I add custom message after HTTP status code ?

推荐答案

您可以让 HTTP 响应的正文包含一条消息,您可以使用任何其他信息对其进行解析.

You could have the body of the HTTP response contain a message which you can parse with any additional information.

状态代码(在响应中)上的 HTTP 状态消息可以是您想要的任何内容,并且不会影响任何客户端.HTTP 客户端通常会忽略消息文本.

The HTTP status message on the status code (in the response) can be anything you want and it won't effect any clients. HTTP clients usually ignore the message text.

这是一个替代方案.创建一个带有状态代码和消息的通用异常.然后创建一个异常处理程序.使用异常处理程序从异常中取出信息并返回给服务的调用者.

Here is an alternative. Create a generic exception that takes a status code and a message. Then create an exception handler. Use the exception handler to retrieve the information out of the exception and return to the caller of the service.

http://javaninja.net/2016/06/throwing-exceptions-messages-spring-mvc-controller/

public class ResourceException extends RuntimeException {

    private HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    /**
     * Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()}
     *                method.
     */
    public ResourceException(HttpStatus httpStatus, String message) {
        super(message);
        this.httpStatus = httpStatus;
    }
}

然后使用异常处理程序检索信息并将其返回给服务调用者.

Then use an exception handler to retrieve the information and return it to the service caller.

@ControllerAdvice
public class ExceptionHandlerAdvice { 

    @ExceptionHandler(ResourceException.class)
    public ResponseEntity handleException(ResourceException e) {
        // log exception 
        return ResponseEntity.status(e.getHttpStatus()).body(e.getMessage());
    }         
} 

然后在需要时创建一个例外.

Then create an exception when you need to.

throw new ResourceException(HttpStatus.NOT_FOUND, "We were unable to find the specified resource.");

这篇关于如何在 HTTP 状态码后添加自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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