Java 未根据Spring 4.3.0中的自定义响应类转换响应
我扩展了ResponseEntity类,并用构造函数创建了一个自定义类Java 未根据Spring 4.3.0中的自定义响应类转换响应,java,json,spring,spring-mvc,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":
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());
}
}
@RequestMapping(value=“/getcommunity”)
public@responseBy CultureRiderResponseEntity getCommunity(){
试一试{
List communityTypeList=iCommunityTypeService.getList();
if(communityTypeList!=null&&communityTypeList.size()>0)
返回新的CultureRiderResponseEntity(HttpStatus.OK,communityTypeList);
返回新的CultureResponseEntity(HttpStatus.NO_CONTENT,CultureDisconstant.NO_COMMUNITY);
}捕获(培养物异常){
返回新的CultureRiderResponseEntity(HttpStatus.EXPECTATION_失败,例如errorMessage());
}
}
响应实体类是我为扩展响应实体而创建的:
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;
}
}
公共类CultureResponseEntity扩展了ResponseEntity{
//字符串属性用于存储错误消息
私有字符串消息;
公共文化响应实体(HttpStatus状态、对象数据){
超级(数据、状态);
调试(“响应数据:”,数据);
}
公共文化响应实体(HttpStatus状态,异常ex){
超级(空,状态);
message=ex.getMessage();
log.error(例如getMessage(),例如);
}
公共文化响应实体(HttpStatus状态,字符串msg){
超级(空,状态);
设置消息(msg);
调试(“响应数据:”,msg);
}
公共文化响应实体(HttpStatus状态、对象数据、字符串消息){
超级(数据、状态);
设置消息(msg);
调试(“响应数据:”,数据);
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
}
为什么要延长响应时间,这似乎是一件奇怪的事情
在任何情况下,ResponseEntity(HttpEntity)在Spring4中都有一个getHeaders(),它似乎被序列化为JSON的一部分
看起来创建扩展是为了方便错误处理,这通常是使用@ExceptionHandler注释的方法完成的,并返回另一个带有适当错误代码的ResponseEntity。为什么要扩展ResponseEntity,这看起来很奇怪 在任何情况下,ResponseEntity(HttpEntity)在Spring4中都有一个getHeaders(),它似乎被序列化为JSON的一部分
看起来创建扩展是为了方便错误处理,这通常是使用@ExceptionHandler注释方法完成的,并返回另一个带有适当错误代码的ResponseEntity。我创建了自己的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;
}
}
我创建了自己的cutome类,没有扩展响应。现在我想要的结果是好的
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;
}
}
问题在哪里?请提供您的
RequestMapping
方法和控制器类also@RamanujanR我添加了控制器方法和响应实体类。请看一看。spring 3的次要版本是什么?@Zorglube我的问题是->我希望spring 4会像spring 3一样返回JSON数据。问题在哪里?请提供您的RequestMapping
方法和控制器类also@RamanujanR我添加了控制器方法和响应实体类。请看一看。Spring3的次要版本是什么?@Zorglube我的问题是->我想Spring4会像Spring3一样返回JSON数据。