向Spring ResponseEntity添加一个新的Header

时间:2017-08-21 08:20:26

标签: java spring http http-headers httpresponse

如何向 org.springframework.http.ResponseEntity 对象添加新标头?

这是我的代码:

df[df_filter < 5]

当我尝试上面的代码时,我得到了

  

java.lang.UnsupportedOperationException

2 个答案:

答案 0 :(得分:0)

这是我建议您实施的:

@GetMapping(value = "/get-products-detail/", produces = "application/json; charset=UTF-8")
    public ResponseEntity<Object> getProductsDetail(@RequestParam Long userToken, @RequestParam Long productId) {
        try {
            return ResponseEntity.ok().body(digitoonContentService.getProductsDetails(userToken, productId));
        } catch (InvalidTokenException e) {
            logger.info("token is null or invalid", e);
            MultiValueMap<String, String> headers = new HttpHeaders();
            headers.add("invalid_token", "true");
            ErrorDTO errorDTO = new ErrorDTO(HttpStatus.UNAUTHORIZED, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
            return new ResponseEntity<Object>(errorDTO, headers, HttpStatus.UNAUTHORIZED);
        }

答案 1 :(得分:0)

您需要立即创建一个。

   final HttpHeaders responseHeaders = new HttpHeaders();
   responseHeaders.set(headerName, headerValue);
   final ResponseEntity<> response = new ResponseEntity<>(responseHeaders, HttpStatus.FORBIDDEN);