【问题标题】:why does the program need to check API header "Accept"为什么程序需要检查 API 标头“Accept”
【发布时间】:2021-07-21 15:10:06
【问题描述】:

我正在使用 swagger codegen 为我生成控制器。那里有一个 if 语句,但在我看来这是没有必要的。谢谢。

        String accept = request.getHeader("Accept");
        if (accept != null && accept.contains("application/json"))

【问题讨论】:

  • 您能解释一下Accept 标头的作用吗?
  • 为什么你认为没有必要?它检查Accept 标头存在和application/json 的条件。这对于基于 json 的 api 来说是相当标准的。如果你不需要它,那么你可以删除它。
  • 但是有一个header ''Content-Type"。为什么我还需要检查它?

标签: java spring api swagger codegen


【解决方案1】:

您的 swagger 文档必须具有类似于以下 API 方法的内容:

responses:
   "200":
     description: OK
     content:
       application/json:

据此,您的 API 的响应类型为 application/json:。但另外在未来,如果服务器决定产生一些其他类型的响应也如下:

 responses:
   "200":
     content:
       image/svg+xml:
         schema:
           type: string
       application/json:
         schema:
           $ref: "#/components/schemas/MyDto"

在这种情况下,需要在响应 Accept 参数中决定响应类型。所以在我看来,这种情况的产生有两个原因:

  1. 客户端和服务器在返回内容方面具有相同的合同。

  2. 如果明天添加新的内容类型,旧代码不会被破坏。

【讨论】:

    【解决方案2】:

    Content-Type:它是一个标头,用于说明 HTTP 消息(请求和响应)中发送的数据格式。

    Accept:当从浏览器(客户端)请求到 Web 服务器时,它是放置在请求中的标头。 这个 Accept 标头仅仅意味着我(客户端)将只允许您将此数据类型作为响应。

    如果服务器支持多种数据类型,服务器使用accept这样的标头确定响应的数据类型,

    @GetMapping("/someresources")
    public ResponseEntity<String> getSomeresources(@RequestHeader("accept") String accept) {
            
            List<SomeResource> someresources = someService.someresources();
            
            //for react app
            if(accept.contains("application/json")) {
                SomeresourcesRepresentation representation = new SomeresourcesRepresentation(someresources);
                String serialziedRepresentaiton = jsonSerializer.serialize(representation);
                
                ResponseEntity<String> response = ResponseEntity
                        .status(HttpStatus.OK)
                        .contentType(MediaType.APPLICATION_JSON)
                        .body(serialziedRepresentaiton);
                
                return response;
            } 
    
            //for web browser
            if(accept.contains("text/html")) {
                
                String html = "<!doctype html>"
                        + "<html>"
                        + "<head>"
                        + "<meta charset='UTF-8'>"
                        + "<title>summary</title>"
                        + "</head>"
                        + "<body>"
                        + "someresources size : "+someresources.size()
                        + "</body>"
                        + "</html>";
                
                ResponseEntity<String> response = ResponseEntity
                        .status(HttpStatus.OK)
                        .contentType(MediaType.TEXT_HTML)
                        .body(html);
    
                return response;
            }
            
            return ResponseEntity.notFound().build();
        }
    
    

    【讨论】:

      猜你喜欢
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      相关资源
      最近更新 更多