java.lang.UnsupportedOperationException:在Spring中使用响应实体设置标头时为null

时间:2019-05-12 07:26:48

标签: spring spring-boot spring-mvc

我正在调用一个Web服务,该服务将返回我的ResponseEntity对象。现在,我在从ResponseEntity获取HttpHeaders信息后设置位置标头。

    @background(schedule=5)
    def date_check(van_used, trip_start, trip_end):
        """Will be used as a background task to make sure trips that have ended don't hog van availability."""
        today = datetime.date.today()
        name = van_used
        if today > trip_start and today > trip_end:
            vans.objects.filter(vanName = name).update(available = True)

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        self.date_check(self.van_used, self.trip_start, self.trip_end, repeat=Task.DAILY)

上面的代码行抛出java.lang.UnsupportedOperationException,因为标头不可修改。在这种特殊情况下,如何设置位置标题?

1 个答案:

答案 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);
        }