代码之家  ›  专栏  ›  技术社区  ›  italktothewind

Spring的restemplate:查询参数的复杂对象

  •  0
  • italktothewind  · 技术社区  · 7 年前

    我有一个复杂的物体:

    public class ComplexObject {
    
        private String a;
        private String b; 
        ...
        private String z;
    
        //getters and setters
    }
    

    我想调用接收所有复杂对象字段的web服务: http://localhost:8080/api/some_service?a=something&b=something&...&z=something

    0 回复  |  直到 7 年前
        1
  •  2
  •   Vivek    7 年前

    对!有一种方法可以传递完整的复杂对象来进行服务调用,然后确保它可以自动实现。

    public HttpStatus send() 
    {
    ComplexObject complexObj = getYourFilledObject();
    
    
    ResponseEntity<HttpStatus> response = restTemplate.postForEntity(ROOT_URI, complexObj, HttpStatus.class);
    
    return response;
    

    }

    如果没有,而且GET是唯一的选择,那么不幸的是,你必须以你自己的身份发送。因为在一天结束时,要么使用rest templates s函数(它接受params映射),要么使用params创建自己的URI,这是同一个httpget,必须以编程方式实现。

    有关示例和插图,请访问 here 最好的参考是春天 resttemplate 医生