我有以下curl表达式:
curl --data 'api_key=API_Key' --data-urlencode 'event=[{"user_id":"12345", "event_type":"buy_song"}]' https://someapi
应转换为RestTemplate.postForEntity调用。我是这样转换的:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("api_key", "API_Key");
params.add("event", URLEncoder.encode(objectMapper.writeValueAsString(Collections.singletonList(e)), "UTF-8"));
// send
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
ResponseEntity<String> response = restTemplate.postForEntity("https://someapi", request, String.class);
服务器返回 400错误的请求
我确认杰克逊的对象映射器正确地序列化了对象objectMapper.writeValueAsString(Collections.singletonList(e))
我怀疑我无法从RestTemplate中的示例curl中正确处理--data
和--data-urlencode
的混合。
请问我在做什么错?
答案 0 :(得分:1)
// org.apache.commons.collections.map.HashedMap
HashedMap requestBody = new HashedMap();
requestBody.put("api_key", "API_Key");
requestBody.put("event", URLEncoder.encode(objectMapper.writeValueAsString(Collections.singletonList(e)), "UTF-8"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
String jsonBody = new ObjectMapper().writeValueAsString(requestBody);
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
ResponseEntity<String> response = restTemplate.postForEntity("https://someapi", entity, String.class);
答案 1 :(得分:1)
对我来说很好:
这是客户端和请求的代码,请忽略请求签名
curl -X GET \
http://localhost:8080/article/so \
-H 'cache-control: no-cache' \
-d 'api_key=API_Key&event=%5B%7B%22user_id%22%3A%2212345%22%2C%20%22event_type%22%3A%22buy_song%22%7D%5D'
控制器: 首次请求将触发您的代码:
// This is just to trigger, you can ignore it.
@RequestMapping(value = "/article/so", method = RequestMethod.GET)
public void addArticle1() throws UnsupportedEncodingException, JsonProcessingException {
articleServiceImpl.test();
}
以上请求将转到与您的代码相同的服务层。
public void test() throws JsonProcessingException, UnsupportedEncodingException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
Event e = new Event();
e.setEvent_type("buy_song");
e.setUser_id("12345");
params.add("api_key", "API_Key");
String encode = URLEncoder.encode(objectMapper.writeValueAsString(Collections.singletonList(e)), "UTF-8");
params.add("event", encode);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/article/so", request, String.class);
System.out.println(response);//<200,[Content-Length:"0", Date:"Fri, 31 May 2019 09:26:24 GMT"]>
}
然后再次使用控制器,只是检查控制器是否从调用方接受String对象或Event对象。在这里,事件作为URLEncodedString传递,我在这里将其作为输出。
@RequestMapping(value = "/article/so", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void addArticle2(@RequestParam String api_key,@RequestParam String event) {
System.out.println(api_key); // API_Key
System.out.println(event); // %5B%7B%22user_id%22%3A%2212345%22%2C%22event_type%22%3A%22buy_song%22%7D%5D
}
答案 2 :(得分:1)
我认为唯一的问题是MediaType
,您发送的数据不是数据(APPLICATION_FORM_URLENCODED
)
它是json数据,因此您需要使用MediaType.APPLICATION_JSON
这样的东西
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// converting form variable to Map
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("api_key", "API_Key");
map.add("event", URLEncoder.encode(objectMapper.writeValueAsString(Collections.singletonList(e)), "UTF-8"));
// finally build Request
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(
apiUrl, request , String.class);
有关{RestTemplate的更多信息,请参见this