需要关于带有正文参数的

需要关于带有正文参数的

本文介绍了需要关于带有正文参数的 RestTemplate Post Request 的帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rest api url,并通过 Rest Client (restclient-ui-2.4-jar-with-dependencies) 提交了与带有正文(用户名、密码、其他参数)的 POST 请求相同的请求,并且它在没有任何问题.

I have a rest api url and submitted the same as POST request with body (user name, password, other parameters) via Rest Client (restclient-ui-2.4-jar-with-dependencies) and it got worked fine without any issues.

例如:

网址:https://test.com/cgi-bin/testing/api正文:username=testuser&password=pass123&id=13002&name=raju

URL: https://test.com/cgi-bin/testing/apiBody: username=testuser&password=pass123&id=13002&name=raju

当我使用 Spring RestTemplate postForObject(url, varmap, Employee.class) 方法时,同样无法正常工作.

The same is not working fine when i used Spring RestTemplate postForObject(url, varmap, Employee.class) method.

有人可以帮我举一个简单的例子,其中请求是一个 URL,带有正文参数,响应是用类映射的 XML?

Can someone help me with a simple example where the request is a URL, with body parameters and the response is XML which is mapped with a class?

示例代码:

  MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
  map.add("username", "test");
  map.add("password", "test123");
  map.add("id", "1234");
  MarshallingHttpMessageConverter mc = new MarshallingHttpMessageConverter();
  mc.setMarshaller(new Jaxb2Marshaller());
  mc.setUnmarshaller(new Jaxb2Marshaller());
  list.add(marshallingHttpMessageConverter);
  emediateRestTemplate.setMessageConverters(list);
  Employee employee = (Employee) restTemplate.postForObject(url, map, Employee.class);

提前致谢,凯瑟尔

推荐答案

上述转换器 Ex: "MarshallingHttpMessageConverter" 不是必需的.

The above converters Ex: "MarshallingHttpMessageConverter" are not required.

MultiValueMap<String, String> parametersMap = new LinkedMultiValueMap<String, String>();
parametersMap.add("username", "test");
parametersMap.add("password", "test123");
parametersMap.add("id", "1234");

对于帖子:

restTemplate.postForObject(url, parametersMap, Employee.class);

  • url 是 String - rest api URL
  • parametersMap - MultiValueMap
  • Employee - 需要从 JSON 响应转换的对象
  • 获取:

    restTemplate.getForObject(url,  class object, variablesMap);
    

    • url is : String - rest api URL
    • variablesMap - 地图
    • class object - 需要从 JSON 响应转换的对象
    • 这篇关于需要关于带有正文参数的 RestTemplate Post Request 的帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:24