Java Spring RestTemplate Post 收到 500 内部服务器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26660030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:01:21 来源:igfitidea点击:
Spring RestTemplate Post getting 500 internal server error
提问by Hrishikesh
Hi I'm new to Spring framework.I have written app which post object to RESTAPI using Resttemplate but i am getting this error
嗨,我是 Spring 框架的新手。我编写了使用 Resttemplate 将对象发布到 RESTAPI 的应用程序,但我收到此错误
23:07:05.856 [main] DEBUG o.s.web.client.RestTemplate - Created POST request for
"http://dummyurl.net/Index"
23:07:05.887 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept
header to [application/json, application/*+json]
23:07:05.887 [main] DEBUG o.s.web.client.RestTemplate - Writing
[org.hrishi.ConsumeReStApi.Info@727803de] as "application/json" using
[org.springframework.http.converter.json.MappingHymansonHttpMessageConverter@704921a5]
23:07:05.981 [main] WARN o.s.web.client.RestTemplate - POST request for "dummyurl.net"
resulted in 500 (Internal Server Error); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:598)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:556)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:363)
at org.hrishi.ConsumeReStApi.App.main(App.java:35)
and this my code
这是我的代码
String url= "http://dummyurl.net/Index";
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/json");
Info info1=new Info();
info1.Id="1711";
info1.Name="Hrishi";
HttpEntity<Info> HReq=new HttpEntity<Info>(info1,headers);
ResponseEntity<Info> info = restTemplate.postForEntity(url, HReq, Info.class);
System.out.print("id : "+info.getBody());
and this is request object
这是请求对象
public class Info {
public String Id;
public String Name;
public String getName() {
return Name;
}
public String getId() {
return Id;
}
}
回答by Naag
You can try the below code:
你可以试试下面的代码:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Info {
@JsonProperty("Id")
public String Id;
@JsonProperty("Id")
public String Name;
public String getName() {
return Name;
}
public String getId() {
return Id;
}