Spring 实体的REST模板发布
调用了我的post方法,但我的配置文件为空。这种方法有什么问题?我必须使用@Requestbody才能使用RestTemplate吗Spring 实体的REST模板发布,spring,spring-mvc,spring-3,Spring,Spring Mvc,Spring 3,调用了我的post方法,但我的配置文件为空。这种方法有什么问题?我必须使用@Requestbody才能使用RestTemplate吗 Profile profile = new Profile(); profile.setEmail(email); String response = restTemplate.postForObject("http://localhost:8080/user/", profile, String.class); @RequestMapping
Profile profile = new Profile();
profile.setEmail(email);
String response = restTemplate.postForObject("http://localhost:8080/user/", profile, String.class);
@RequestMapping(value = "/", method = RequestMethod.POST)
public @ResponseBody
Object postUser(@Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {
//Profile is null
return profile;
}
您必须以这种方式构建概要文件对象
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("email", email);
Object response = restTemplate.postForObject("http://localhost:8080/user/", parts, String.class);
MultiValueMap parts=新链接的MultiValueMap();
部分。添加(“电子邮件”,电子邮件);
对象响应=restTemplate.postForObject(“http://localhost:8080/user/“、部件、字符串、类);
多值映射
对我来说是一个很好的起点,但在我的情况下,它仍然将空对象发布到@RestController
我的实体创建和发布解决方案最终看起来是这样的:
HashedMap requestBody=newhashedmap();
put(“eventType”、“testDeliveryEvent”);
requestBody.put(“sendType”、“SINGLE”);
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//Jackson ObjectMapper将requestBody转换为JSON
字符串json=newObjectMapper().writeValueAsString(请求);
HttpEntity=新的HttpEntity(json,标题);
restemplate.postForEntity(“/generate”,entity,String.class);
我目前的做法:
final Person=Person.builder().name(“antonio”).build();
最终响应Entity response=restTemplate.postForEntity(
新网址(“http://localhost:“+port+”/person/aggregate”).toString(),
人,人,阶级),;
您的控制器是否注释为在@RequestMapping
中包含路径的用户部分?因为您的metohd注释指向/
,如果没有附加的控制器注释,它将不会响应/user/
。@nicholas.hauschild Yes。我正在输入控制器方法。问题在于,在实际方法中,概要文件是空的。