Home > Back-end >  Help!!!!! Spring the boot should be associated to the LinkedHashMap RestTemplate put the Response ob
Help!!!!! Spring the boot should be associated to the LinkedHashMap RestTemplate put the Response ob

Time:11-05

With spring boot made two apis, between two APi call using the RestTemplate postForObject method, the return type is a custom ApiResponse,

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:

 ParameterizedTypeReference TypeRef=new ParameterizedTypeReference () {
};
ResponseEntity ResponseEntity=restTemplate. Exchange (url, HttpMethod. POST, new HttpEntity<> (MVM), typeRef);
List MyModelClasses=responseEntity. GetBody ();
add a ParameterizedTypeReference specified type
  • Related