【问题标题】:Spring RestTemplate Post getting 500 internal server errorSpring RestTemplate Post 得到 500 内部服务器错误
【发布时间】:2014-12-26 21:49:21
【问题描述】:

您好,我是 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.MappingJacksonHttpMessageConverter@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)

这是我的代码

        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());

这是请求对象

public class Info {

    public String Id;
    public String Name;

    public String getName() {
        return Name;
    }

    public String getId() {
        return Id;
    }

}

【问题讨论】:

  • 你能发布服务器端代码(只有api),因为根据日志它会抛出500,所以可能是rest api问题?
  • 发布@Controller@RequestMapping 代码。
  • 不好的做法有实例变量public,它们必须是private,这就是你使用getter/setter的原因。
  • 实例变量名应该以小写开头,而不是大写。
  • 既然你得到 HTTP 状态码 500 那么这意味着它是一个服务器错误,而不是客户端错误。

标签: java json spring spring-mvc


【解决方案1】:

你可以试试下面的代码:

@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;
}

【讨论】:

    【解决方案2】:

    好像你没有用'application/json'正确设置标题内容类型'

          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
    
          HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
          restTemplate.put(uRL, entity);
    

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 2014-09-24
      • 2020-10-01
      • 2018-01-15
      相关资源
      最近更新 更多