REST POST可与POSTMAN一起正常使用,但在使用Spring RestTemplate时会出现异常 [英] REST POST works correctly with POSTMAN but exception when using Spring RestTemplate

查看:1219
本文介绍了REST POST可与POSTMAN一起正常使用,但在使用Spring RestTemplate时会出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Rest客户端,以使用Spring RestTemplate发布JSON数据. 使用POSTMAN并遵循正文中的JSON数据可正确获取响应-

I am writing a Rest client to post JSON data using Spring RestTemplate. Using POSTMAN and following JSON data in body get the response correctly-

{
    "InCode":"test",
    "Name":"This is  test",
    "Email":"test@gmail.com",
    "Id":18,
}

但是,当尝试使用Spring RestTemplate按下REST API时,如下所示

However when trying to hit the REST API using Spring RestTemplate as follows

ResponseEntity<String> response = restTemplate.exchange(baseUrl,
                HttpMethod.POST, getHeaders(), String.class);

private HttpEntity<?> getHeaders() throws JSONException {
JSONObject request = new JSONObject();
        request.put("Email", "test@gmail.com");
        request.put("Id", "18");
        request.put("Name", "This is  test");
        request.put("InCode", "test");

        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
        return new HttpEntity<>(request.toString(), headers);
        }

我得到了例外-

11:52:56.808 [main] DEBUG o.s.web.client.RestTemplate - Created POST request for "http://server-test/platform/v4/org"
11:52:56.815 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*]
12:03:47.357 [main] DEBUG o.s.web.client.RestTemplate - Writing [{"InCode":"test","Email":"test@gmail.com","Id":"18","Name":"This is  test"}] using [org.springframework.http.converter.StringHttpMessageConverter@6a1aab78]
11:52:57.574 [main] DEBUG o.s.web.client.RestTemplate - POST request for "http://server-test/platform/v4/org" 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.handleResponse(RestTemplate.java:641)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)

感谢您的帮助.

推荐答案

您正在设置"Accept"标头,该标头定义了您将接受作为响应的内容类型.

You are setting the 'Accept' header, which defines which content type you will accept as a response.

相反,您必须将标头"Content-Type"设置为"application/json".

Instead you must set the header 'Content-Type' with 'application/json'.

在您的Java代码ID中是一个字符串,在邮递员中是一个数字. 可能这会使服务器发生故障吗?

In your java code id is a string, in the postman its a number. May be this makes the server fail?

这篇关于REST POST可与POSTMAN一起正常使用,但在使用Spring RestTemplate时会出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆