gpt4 book ai didi

java - 在 POST 方法中将字符串发送到 Spring(Jackson) REST Controller

转载 作者:行者123 更新时间:2023-11-30 10:16:19 24 4
gpt4 key购买 nike

我正在尝试使用 HTTP Post 方法将 String 对象发送到休息服务。并且字符串应该在请求正文中发送。

Controller

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

}

测试程序

  1. 首先运行服务器 ( http://localhost:55055/ )
  2. 等到服务器启动并运行。
  3. 运行 test_postType1_() 将按预期工作。
  4. 运行 test_postStringAsText() 将导致客户端/消费者端错误

org.springframework.web.client.HttpClientErrorException: 415 null

at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684) at org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:405) at personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumer.postStringAsText(HttpMethodPostConsumer.java:21) at personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumerTest.test_postStringAsText(HttpMethodPostConsumerTest.java:17)

  1. 运行 test_postStringAsJSON() 将导致错误
    • 客户端/消费者端错误

org.springframework.web.client.HttpClientErrorException: 400 null

at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684) at org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:405) at personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumer.postStringAsJSON(HttpMethodPostConsumer.java:31) at personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumerTest.test_postStringAsJSON(HttpMethodPostConsumerTest.java:25)

服务器端错误(在控制台中)

02-May-2018 17:25:45.469 WARNING [http-nio-55055-exec-2] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'IAtQDIxTCh': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'IAtQDIxTCh': was expecting 'null', 'true', 'false' or NaN at [Source: (PushbackInputStream); line: 1, column: 21]

服务器端错误(在 Tomcat Catalina 日志中)

02-May-2018 17:25:45.469 WARNING [http-nio-55055-exec-2] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'IAtQDIxTCh': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'IAtQDIxTCh': was expecting 'null', 'true', 'false' or NaN at [Source: (PushbackInputStream); line: 1, column: 21]

最佳答案

如果你想使用@RequestBody POST 到你的服务器,有必要发送一个对象到你的 Controller 。

使用您的字符串参数创建一个 DTO 类:

public class ExampleDTO {

private String stringExample;

public String getStringExample() {
return stringExample;
}

public void setStringExample(String stringExample) {
this.stringExample= stringExample;
}

你的 Controller :

@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")

关于java - 在 POST 方法中将字符串发送到 Spring(Jackson) REST Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50136495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com