我正在尝试使用HTTP Post方法将String
对象发送到休息服务。并且String应该在请求体中发送。
控制器
@RestController
@RequestMapping(value = "post", method = RequestMethod.POST)
public class HttpMethodPostController {
/*HttpClientErrorException: 415 null*/
@RequestMapping(value = "/string_as_text", consumes = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<Void> getStringAsText(@RequestBody String text) {
System.out.println("[Server] text = " + text);
return new ResponseEntity<Void>(HttpStatus.OK);
}
/*HttpClientErrorException: 400 null*/
@RequestMapping(value = "/string_as_json", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Void> getStringAsJSON(@RequestBody String text) {
System.out.println("[Server] text = " + text);
return new ResponseEntity<Void>(HttpStatus.OK);
}
@RequestMapping(value = "/type1_", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Void> postType1_(@RequestBody Type1_ type1_) {
System.out.println("[Server] type1_ = " + type1_);
return new ResponseEntity<Void>(HttpStatus.OK);
}
}
消费者
public class HttpMethodPostConsumer {
public static final String POST_ADDRESS = "http://localhost:55055/post";
/*HttpClientErrorException: 415 null*/
public static void postStringAsText(String text) {
final RestTemplate restTemplate = new RestTemplate();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
final HttpEntity<String> entity = new HttpEntity<>(text, headers);
final URI uri = restTemplate.postForLocation(POST_ADDRESS + "/string_as_text", entity, String.class);
System.out.println("uri = " + uri);
}
/*HttpClientErrorException: 400 null*/
public static void postStringAsJSON(String text) {
final RestTemplate restTemplate = new RestTemplate();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
final HttpEntity<String> entity = new HttpEntity<>(text, headers);
final URI uri = restTemplate.postForLocation(POST_ADDRESS + "/string_as_json", entity, String.class);
System.out.println("uri = " + uri);
}
public static void postType1_(Type1_ type1_) {
final RestTemplate restTemplate = new RestTemplate();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
final HttpEntity<Type1_> httpEntity = new HttpEntity<Type1_>(type1_, headers);
final URI result_URI = restTemplate.postForLocation(POST_ADDRESS + "/type1_", httpEntity, Type1_.class);
System.out.println("result_URI = " + result_URI);
}
}
消费者测试运动员
public class HttpMethodPostConsumerTest {
/*HttpClientErrorException: 415 null*/
@Test
public void test_postStringAsText() {
final String text = Randomizers.getString();
System.out.println("text = " + text);
postStringAsText(text);
}
/*HttpClientErrorException: 400 null*/
@Test
public void test_postStringAsJSON() {
final String text = Randomizers.getString();
System.out.println("text = " + text);
postStringAsJSON(text);
}
@Test
public void test_postType1_() {
final Type1_ type1_ = ModelFactory.getType1_();
System.out.println("[Consumer] type1_ = " + type1_);
postType1_(type1_);
}
}
测试程序
test_postType1_()
将按预期工作。test_postStringAsText()
将导致客户端/用户端错误org.springframework.web.client.HttpClientErrorException:415 null
在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) 在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) 在 org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) 在 org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775) 在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728) 在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684) 在 org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:405) 在 personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumer.postStringAsText(HttpMethodPostConsumer.java:21) 在 personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumerTest.test_postStringAsText(HttpMethodPostConsumerTest.java:17)
test_postStringAsJSON()
会导致错误
org.springframework.web.client.HttpClientErrorException:400 null
在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) 在 org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) 在 org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) 在 org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775) 在 org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728) 在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684) 在 org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:405) 在 personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumer.postStringAsJSON(HttpMethodPostConsumer.java:31) 在 personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumerTest.test_postStringAsJSON(HttpMethodPostConsumerTest.java:25)
服务器端错误(在控制台中)
02-May-2018 17:25:45.469警告[http-nio-55055-exec-2] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable 无法读取HTTP消息: org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误:无法识别的令牌&#39; IAtQDIxTCh:正在等待 &#39; null&#39;,&#39; true&#39;,&#39; false&#39;或NaN;嵌套异常是 com.fasterxml.jackson.core.JsonParseException:无法识别的令牌 &#39; IAtQDIxTCh&#39;:期待&#39; null&#39;&#39; true&#39;&#39; false&#39;或NaN at [来源:( PushbackInputStream); line:1,column:21]
服务器端错误(在Tomcat Catalina日志中)
02-May-2018 17:25:45.469警告[http-nio-55055-exec-2] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable 无法读取HTTP消息: org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误:无法识别的令牌&#39; IAtQDIxTCh:正在等待 &#39; null&#39;,&#39; true&#39;,&#39; false&#39;或NaN;嵌套异常是 com.fasterxml.jackson.core.JsonParseException:无法识别的令牌 &#39; IAtQDIxTCh&#39;:期待&#39; null&#39;&#39; true&#39;&#39; false&#39;或NaN at [来源:( PushbackInputStream); line:1,column:21]
答案 0 :(得分:1)
如果您想使用@RequestBody发送到服务器,则必须将对象发送到控制器。
使用String参数创建DTO类:
public class ExampleDTO {
private String stringExample;
public String getStringExample() {
return stringExample;
}
public void setStringExample(String stringExample) {
this.stringExample= stringExample;
}
}
你的控制器:
@RequestMapping(method = RequestMethod.POST, value = "/string_as_text")
public ResponseEntity<Void> getStringAsText(@RequestBody ExampleDTO exampleDTO) {
System.out.println("[Server] text = " + exampleDTO.getStringExample());
return new ResponseEntity<Void>(HttpStatus.OK);
}
消费者
ExampleDTO param = new ExampleDTO();
param.setStringExample("text")