Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring/RestTemplate-将实体放入服务器_Java_Json_Spring_Put_Resttemplate - Fatal编程技术网

Java Spring/RestTemplate-将实体放入服务器

Java Spring/RestTemplate-将实体放入服务器,java,json,spring,put,resttemplate,Java,Json,Spring,Put,Resttemplate,请看以下简单代码: final String url = String.format("%s/api/shop", Global.webserviceUrl); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); HttpHeaders headers = new HttpHeade

请看以下简单代码:

final String url = String.format("%s/api/shop", Global.webserviceUrl);

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpHeaders headers = new HttpHeaders();
headers.set("X-TP-DeviceID", Global.deviceID);
HttpEntity entity = new HttpEntity(headers);

HttpEntity<Shop[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Shop[].class);
shops = response.getBody();
final String url=String.format(“%s/api/shop”,Global.webserviceUrl);
RestTemplate RestTemplate=新RestTemplate();
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
HttpHeaders=新的HttpHeaders();
headers.set(“X-TP-DeviceID”,Global.DeviceID);
HttpEntity=新的HttpEntity(标题);
HttpEntity响应=restTemplate.exchange(url,HttpMethod.GET,entity,Shop[].class);
shops=response.getBody();
如您所见,上述代码旨在从服务器获取商店列表(json格式),并将响应映射到商店对象数组。 现在我需要添加新的shop,例如/api/shop/1。请求实体应具有与返回实体完全相同的格式

我是否应该将/1添加到我的url中,创建新的Shop类对象,并将所有字段填入我要输入的值,然后使用exchange with HttpMethod.put

请给我澄清一下,我是Spring的初学者。代码示例将不胜感激

[编辑]
我感到双重困惑,因为我刚刚注意到方法restemplate.put()。那么,我应该用哪一个呢?Exchange或put()?

您可以尝试以下操作:

    final String url = String.format("%s/api/shop/{id}", Global.webserviceUrl);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpHeaders headers = new HttpHeaders();
    headers.set("X-TP-DeviceID", Global.deviceID);
    Shop shop= new Shop();
    Map<String, String> param = new HashMap<String, String>();
    param.put("id","10")
    HttpEntity<Shop> requestEntity = new HttpEntity<Shop>(shop, headers);
    HttpEntity<Shop[]> response = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Shop[].class, param);

    shops = response.getBody();
final String url=String.format(“%s/api/shop/{id}”,Global.webserviceUrl);
RestTemplate RestTemplate=新RestTemplate();
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
HttpHeaders=新的HttpHeaders();
headers.set(“X-TP-DeviceID”,Global.DeviceID);
商店=新商店();
Map param=new HashMap();
参数put(“id”,“10”)
HttpEntity requestEntity=新的HttpEntity(车间、标题);
HttpEntity response=restTemplate.exchange(url、HttpMethod.PUT、requestEntity、Shop[].class、param);
shops=response.getBody();

put返回void,而exchange会给您一个响应,检查文档是最好的地方

我想我应该使用requestEntity而不是entity作为exchange参数?另外,为什么id是hasmap,您的意思是可能有多个url参数?是的,它用于多个url参数。我认为有一个重载版本不需要map,您可以直接传递url参数和query参数(如果需要)。用requestEntity而不是entity更新了答案。答案很棒!我知道这是一个小问题,但请更正这一行:param.put(“id”,“10”),因为这不同于简单的引号字符,我刚刚将代码复制到编辑器中,它显示这些字符是不同的。谢谢兄弟!我不知道,因为RestTemplateHelper中有postEntity、getEntity和patchEntity,而DoeSsent中有putEntity,您可能希望使用POST创建新对象,并使用PUT更新现有对象。