向服务器请求时如何在 RestTemplate 上获取数据 json
How to get data json on RestTemplate when request to server
我正在使用 RestTemplate 和 JSONObject json 向服务器发送 POST 请求。当我尝试向 Postmen 请求时,它 运行 成功并且 return 结果 200(好的),
但是当 运行 我的代码不能 运行
这是我的格式json:
{
"senderUser": "user@gmail.com",
"data": [
{
"actionType": "update-contact",
"data": {
"name": "luong van",
"lastname": "khanh",
"type": 0,
"title": "",
"passport": "",
"gender": 1,
"bgInfo": "",
"dateOfBirth": "",
"emails": [{"value": "user@gmail.com"}],
"phones": [{"value": "0902032618"}],
"addresses": [{"street": "10", "city":"Osaka", "state": "Osake", "country":
{"code":"JP", "name":"Japan"}}],
"tag": ""
}
}
]
}
这是我的 class:
public class InComingAPI {
private static Logger logger = LoggerFactory.getLogger(InComingAPI.class);
public static void main(String []args) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://myservice/90336429462601e7f3326641898fabd9948b349d";
try {
logger.info("hi there");
// RestTemplate restTemplate = new RestTemplate();
// String url = "http://localhost:8181/xyz/updateAdmin";
JSONArray json = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("name", "khanh");
obj.put("lastname", "luong van");
obj.put("type", "0");
obj.put("dateOfBirth", "");
obj.put("emails", "user@gmail.com");
obj.put("phones", "0902032618");
obj.put("addresses", "Osaka");
json.put(obj);
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
String result = restTemplate.postForObject(url, json, String.class);
System.out.println(result);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
它发生如下异常:
2021-01-22 17:50:17,280 INFO [com.outincoming.InComingAPI] hi there
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONArray]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:787)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:566)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:329)
at com.outincoming.InComingAPI.main(InComingAPI.java:70)
我该如何解决这个问题?非常感谢
实际上,抛出的异常完全是误导。原来问题不是 MappingJackson2HttpMessageConverter 不知道如何编组我的对象——这听起来很奇怪,是 JSON-,而是底层 ObjectMapper 的配置。
我所做的就是像那样禁用 属性 SerializationFeature.FAIL_ON_EMPTY_BEANS
public class InComingAPI {
private static Logger logger = LoggerFactory.getLogger(InComingAPI.class);
public static void main(String []args) {
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
String url = "https://myservice/90336429462601e7f3326641898fabd9948b349d";
try {
logger.info("hi there");
// RestTemplate restTemplate = new RestTemplate();
// String url = "http://localhost:8181/xyz/updateAdmin";
JSONArray json = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("name", "khanh");
obj.put("lastname", "luong van");
obj.put("type", "0");
obj.put("dateOfBirth", "");
obj.put("emails", "user@gmail.com");
obj.put("phones", "0902032618");
obj.put("addresses", "Osaka");
json.put(obj);
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
String result = restTemplate.postForObject(url, json, String.class);
System.out.println(result);
}
catch(Exception e) {
e.printStackTrace();
}
}
我正在使用 RestTemplate 和 JSONObject json 向服务器发送 POST 请求。当我尝试向 Postmen 请求时,它 运行 成功并且 return 结果 200(好的), 但是当 运行 我的代码不能 运行
这是我的格式json:
{
"senderUser": "user@gmail.com",
"data": [
{
"actionType": "update-contact",
"data": {
"name": "luong van",
"lastname": "khanh",
"type": 0,
"title": "",
"passport": "",
"gender": 1,
"bgInfo": "",
"dateOfBirth": "",
"emails": [{"value": "user@gmail.com"}],
"phones": [{"value": "0902032618"}],
"addresses": [{"street": "10", "city":"Osaka", "state": "Osake", "country":
{"code":"JP", "name":"Japan"}}],
"tag": ""
}
}
]
}
这是我的 class:
public class InComingAPI {
private static Logger logger = LoggerFactory.getLogger(InComingAPI.class);
public static void main(String []args) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://myservice/90336429462601e7f3326641898fabd9948b349d";
try {
logger.info("hi there");
// RestTemplate restTemplate = new RestTemplate();
// String url = "http://localhost:8181/xyz/updateAdmin";
JSONArray json = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("name", "khanh");
obj.put("lastname", "luong van");
obj.put("type", "0");
obj.put("dateOfBirth", "");
obj.put("emails", "user@gmail.com");
obj.put("phones", "0902032618");
obj.put("addresses", "Osaka");
json.put(obj);
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
String result = restTemplate.postForObject(url, json, String.class);
System.out.println(result);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
它发生如下异常:
2021-01-22 17:50:17,280 INFO [com.outincoming.InComingAPI] hi there
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONArray]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:787)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:566)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:329)
at com.outincoming.InComingAPI.main(InComingAPI.java:70)
我该如何解决这个问题?非常感谢
实际上,抛出的异常完全是误导。原来问题不是 MappingJackson2HttpMessageConverter 不知道如何编组我的对象——这听起来很奇怪,是 JSON-,而是底层 ObjectMapper 的配置。
我所做的就是像那样禁用 属性 SerializationFeature.FAIL_ON_EMPTY_BEANS
public class InComingAPI {
private static Logger logger = LoggerFactory.getLogger(InComingAPI.class);
public static void main(String []args) {
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
String url = "https://myservice/90336429462601e7f3326641898fabd9948b349d";
try {
logger.info("hi there");
// RestTemplate restTemplate = new RestTemplate();
// String url = "http://localhost:8181/xyz/updateAdmin";
JSONArray json = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("name", "khanh");
obj.put("lastname", "luong van");
obj.put("type", "0");
obj.put("dateOfBirth", "");
obj.put("emails", "user@gmail.com");
obj.put("phones", "0902032618");
obj.put("addresses", "Osaka");
json.put(obj);
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
String result = restTemplate.postForObject(url, json, String.class);
System.out.println(result);
}
catch(Exception e) {
e.printStackTrace();
}
}