RestTemplate 返回实体类型

RestTemplate 返回实体默认情况是LinkedHashMap,想要返回实体类需要如下设置:

ParameterizedTypeReference> typeRef = new ParameterizedTypeReference>() {};
ResponseEntity> responseEntity = loadBalanced.exchange(http://127.0.0.1:9103/operation/user,
HttpMethod.POST, null, typeRef);
ResultObject result= responseEntity.getBody();
return result;
/*辅助类如下*/
public class ResultObject implements Serializable {
    private static final long serialVersionUID = -7339247993272106349L;
    /**
     * 1 统一表示成功
     * 其它数值根据自己的业务逻辑枚举
     */
    @JsonProperty("result")
    private long result;
    @JsonProperty("desc")
    private String desc;
    private T body;
    public ResultObject() {
    }
    public ResultObject(long result) {
        this.setResult(result);
    }
    public ResultObject(long result, String desc) {
        this.result = result;
        this.desc = desc;
    }
    public ResultObject(long result, String desc, T body) {
        this.result = result;
        this.desc = desc;
        this.body = body;
    }
    public long getResult() {
        return result;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public T getBody() {
        return body;
    }
    public void setBody(T body) {
        this.body = body;
    }
}
@RequestMapping(value = "/user")
 @ResponseBody
 public  ResultObject user(HttpServletRequest request, Model model) {
  ResultObject result=new ResultObject();
  Operation user = new Operation();
  user.setName("test");
  user.setId(1000l);
  result.setBody(user);
  result.setResult(1);
  result.setDesc("成功");
  return result;
 }
 
  


你可能感兴趣的:(RestTemplate 返回实体类型)