Is called A API Controller
@ Controller
Public class TestController {
@ RequestMapping (value="https://bbs.csdn.net/topics/invocationMethod", method=RequestMethod. POST)
Public ApiResponse getContracts (@ PathVariable Integer id) {
ResultModel model=new ResultModel ();
TestObject object=new TestObject ();
Fild1 object. SetFild1 (" ");
Fild2 object. SetFild2 (" ");
Model. The setId (" 1 ");
Model. The elegantly-named setName (" testName ");
Model. SetObject (object);
Return ApiResponse. Success (model);
}
}
The return type ApiResponse
Public class ApiResponse {
//return code 200: OK
Private Integer statusCode;
//whole message
private String message;
//The component of data
Private Object responseData;
Private ApiResponse (ResultStatus status) {
Enclosing statusCode.=the status code ();
This. Message=status. A tiny ();
}
Public static ApiResponse success (Object responseData) {
ApiResponse response=new ApiResponse (ResultStatus. SUCCESS);
Return the response. SetResponseData (responseData);
}
}
Returns the result ResultModel
Public class ResultModel {
private String id;
Public String getId () {
return id;
}
Public void setId (String id) {
this.id=id;
}
Public String getName () {
return name;
}
Public void elegantly-named setName (String name) {
this.name=name;
}
Public TestObject getObject () {
The return object;
}
Public void setObject (TestObject object) {
Enclosing object=object;
}
private String name;
Private TestObject object;
}
TestObject
Public class TestObject {
Private String fild1;
Private String fild2;
Public String getFild1 () {
Return fild1;
}
Public void setFild1 (String fild1) {
Enclosing fild1=fild1;
}
Public String getFild2 () {
Return fild2;
}
Public void setFild2 (String fild2) {
Enclosing fild2=fild2;
}
}
B Api Model and Api Model are exactly the same,
B Api callback Api
Public class TestService {
The @autowired
Private RestTemplate RestTemplate;
Public ApiResponse callAapi () {
ApiResponse response=
RestTemplate. PostForObject (" http://localhost:9000/invocationMethod ", ", "
ApiResponse. Class);
return response;
}
}
When the ApiResponse ResponseData to ResultModel, wrong
Public ResultModel getResultModel (ApiResponse response) {
Return (ResultModel) response. GetResponseData ();
}
Fault is LinkedHashMap cannot convert ResultModel,
Want to ask about A Api response is actually A json string, through the entity class postForObject into the Bean, why can't the ResponseData in response to the Bean, but has been converted to linkedHashMap?
Investigation for a long time, didn't find the reason, hope to jar the great god, if you have see this post, assessment under the trouble, thank you!!!!!!!!!!
CodePudding user response:
This should go to the source code ~CodePudding user response:
ParameterizedTypeReferenceadd a ParameterizedTypeReference specified typeTypeRef=new ParameterizedTypeReference
() {
};
ResponseEntityResponseEntity=restTemplate. Exchange (url, HttpMethod. POST, new HttpEntity<> (MVM), typeRef);
ListMyModelClasses=responseEntity. GetBody ();