繁体   English   中英

Spring Rest Service RestTemplate PostForEntity两个对象

[英]spring rest service resttemplate postForEntity two objects

我有一个spring rest服务,我有一个带有两个参数Product和Catalog的方法。 方法是

public void addProductCatalog(Product product,Catalog catalog) {
        logger.info("******** ******** CLIENT: addProductCatalog");


        ResponseEntity response = rest.postForEntity(
            "http://localhost:8080/rest/addProductCatalog", 
            product, null,catalog);

调用此方法时出现异常

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:85)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:707)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:660)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:414)
    at com.abc.rest.client.ProductClient.addProductCatalog(ProductClient.java:34)
    at com.abc.InventoryClientApp.run(InventoryClientApp.java:265)
    at com.abc.InventoryClientApp.main(InventoryClientApp.java:24)

如何在postForEntity中设置两个对象?

您的方法调用不正确。创建包含两个对象的公共Pojo。 根据需要设置内容类型,然后进行相应调用。

例如。

public class ProductCatalog{
   private Product product;
   private Catalog catalog;

  // getter and setter. 
}

然后按以下方式调用它:

  restTemplate.postForEntity("http://localhost:8080/rest/addProductCatalog", productCatalog, String.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM