Home > Back-end >  Springboot return map value by ascending order (meaning is carried out in accordance with the order
Springboot return map value by ascending order (meaning is carried out in accordance with the order

Time:01-16

Design the map below:
 map. The put (" 46 ", "daf"); 
The map. The put (" a2 ", "daf");
The map. The put (" 3 ", "daf");
The map. The put (" e1 ", "333");


Expect output in the above order, as a result, unlike expectations,


How to deal with this problem?

The code is as follows:
1, the controller
 @ RestController 
@ RequestMapping ("/test ")
@ Api (tags={} "test map", the description="test map111")
Public class TestController {
@ PostMapping ("/test ")
@ ApiOperation (" MAP ")
Public ResultBody The test () {
The Map Map=new HashedMap ();
The map. The put (" 46 ", "daf");
The map. The put (" a2 ", "daf");
The map. The put (" 3 ", "daf");
The map. The put (" e1 ", "333");
System. The out. Println (map. The size ());
Return new ResultBody (map);
}
}

2, ResultBody
 @ Data 
@ AllArgsConstructor
@ NoArgsConstructor
Public class ResultBody Implements the Serializable {
private String code;
Private String MSG.
Private T data;

Public ResultBody (String code) {
This. Code=code;
This. MSG=the ResultCode. SELECT_SUCCESS. GetCode ();
}

Public ResultBody (String code, String MSG) {
This. Code=code;
This. MSG=MSG;
}

Public ResultBody (T data) {
This. Code=the ResultCode. SELECT_SUCCESS. GetCode ();
This data=https://bbs.csdn.net/topics/data;
This. MSG=the ResultCode. SELECT_SUCCESS. GetMsg ();
}

Public static ResultBody success () {
Return new ResultBody (the ResultCode. SELECT_SUCCESS. GetCode ());
}

Public static ResultBody success (Object data) {
Return new ResultBody (data);
}

Public static ResultBody fail () {
Return new ResultBody (the ResultCode. SELECT_ERROR);
}

Public static ResultBody fail (String code, String MSG) {
Return new ResultBody (code, MSG);
}

Public static & lt; T> ResultBody Failed (IErrorCode errorCode) {
Return new ResultBody (errorCode. GetCode (), errorCode. GetMsg (), null);
}

}

CodePudding user response:

That use LinkHashedMap

CodePudding user response:

LinkHashedMap used, no matter use, I guess is a problem of Jackson, but don't know how to solve,

CodePudding user response:

1. Yourself into a string to the front desk, the automatic conversion of the
no spring2. Change the data structure, with the list
  • Related