响应未根据Spring 4.3.0中的自定义响应类进行转换

时间:2017-01-06 10:30:11

标签: java json spring spring-mvc

我扩展了ResponseEntity类并使用构造函数创建了一个自定义类。

But in Spring 3 and Spring 4 JSON format is different.

1)在春季4:

[
  {
    "communityTypeId": 1,
    "communityTypeName": "Indian"
  }
] 

2)在春季3:

{
  "headers": {},
  "body": [
      {
        "communityTypeId": 1,
        "communityTypeName": "Indian"
      }
   ]
}

我的控制器方法是:

@RequestMapping(value = "/getcommunity")
    public @ResponseBody CultureRideResponseEntity getCommunity() {
        try {
            List<CommunityType> communityTypeList = iCommunityTypeService.getList();
            if (communityTypeList != null && communityTypeList.size() > 0)
                return new CultureRideResponseEntity(HttpStatus.OK, communityTypeList);
            return new CultureRideResponseEntity(HttpStatus.NO_CONTENT, CulturerideConstant.NO_COMMUNITY_FOUND);
        } catch (CultureRideException ex) {
            return new CultureRideResponseEntity(HttpStatus.EXPECTATION_FAILED, ex.errorMessage());
        }
    }

我创建的Response实体类是为了扩展ResponseEntity:

public class CultureRideResponseEntity extends ResponseEntity<Object> {    

    // String Property is used to store error message
    private String message;

    public CultureRideResponseEntity(HttpStatus status, Object data) {
        super(data, status);
        log.debug("response data:", data);
    }

    public CultureRideResponseEntity(HttpStatus status, Exception ex) {
        super(null, status);
        message = ex.getMessage();
        log.error(ex.getMessage(), ex);
    }

    public CultureRideResponseEntity(HttpStatus status, String msg) {
        super(null, status);
        setMessage(msg);
        log.debug("response data:", msg);
    }

    public CultureRideResponseEntity(HttpStatus status, Object data, String msg) {
        super(data, status);
        setMessage(msg);
        log.debug("response data:", data);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

2 个答案:

答案 0 :(得分:2)

为什么你扩展了ResponseEntity,这似乎是一件奇怪的事情?

在任何情况下,ResponseEntity(HttpEntity)在Spring 4中都有一个getHeaders(),这似乎是作为JSON的一部分序列化的。

看起来扩展是为了便于错误处理而创建的,这通常使用@ExceptionHandler带注释的方法完成,并返回另一个带有相应错误代码的ResponseEntity。

答案 1 :(得分:0)

我创建了自己的cutome类而没有扩展ResponseEntity。现在结果很好,我想要的。

public class TestResponseEntity {    

        // String Property is used to store error message
        private String message;

        private Object body;

        private String message;

        public TestResponseEntity(HttpStatus status, Object data) {
            this.statusCode = status.name();
            this.body = data;
            log.debug("response data:", data);
        }

        public TestResponseEntity(HttpStatus status, Exception ex) {
            this.statusCode = status.name();
            this.message = ex.getMessage();
            log.error(ex.getMessage(), ex);
        }

        public TestResponseEntity(HttpStatus status, String msg) {
           this.statusCode = status.name();
           this.message = msg;
            log.debug("response data:", msg);
        }

        public TestResponseEntity(HttpStatus status, Object data, String msg) {
          this.statusCode = status.name();
          this.body = data;
          this.message = msg;
            log.debug("response data:", data);
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }