简体   繁体   English

如何将 JSON 请求解析为 FlightOfferSearch[] object?

[英]How do I parse a JSON request into a FlightOfferSearch[] object?

I have trouble deserializing the Response Example JSON for a FlightOfferSearch from here when passing it to my test REST controller:当我将它传递给我的测试 REST controller 时,我FlightOfferSearch这里反序列化 FlightOfferSearch 的响应示例 JSON:

@RestController
public class MyController {

  @PostMapping("/search")
  String searchHandler(@RequestBody FlightOfferSearch[] flightOfferSearches) {
    return flightOfferSearches[0].toString();
  }
}

(minimal example with just Spring Boot Starter App with "Web" dependency and gson 2.8.5 and amadeus-java 4.1.0 added in the pom.xml) (最小示例,仅 Spring Boot Starter App 与“Web”依赖项和gson 2.8.5amadeus-java 4.1.0添加到 pom.xml)

I'm getting this in IntelliJ:我在 IntelliJ 中得到了这个:

2020-04-20 17:05:59.392  WARN 47240 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Conflicting setter definitions for property "fixedLengthStreamingMode": java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params) vs java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "fixedLengthStreamingMode": java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params) vs java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params)
 at [Source: (PushbackInputStream); line: 1, column: 1]]

Process finished with exit code -1

and this in Postman:这在 Postman 中:

"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Conflicting setter definitions for property \"fixedLengthStreamingMode\": java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params) vs java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property \"fixedLengthStreamingMode\": java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params) vs java.net.HttpURLConnection#setFixedLengthStreamingMode(1 params)\n at [Source: (PushbackInputStream); line: 1, column: 1]"

All comments are appreciated, thanks!感谢所有评论,谢谢!

You probably didn't create setters for some of the fields for your parameter class, but the problem lies deeper - your way of handling this, is incorrect.您可能没有为参数 class 的某些字段创建设置器,但问题更深层次 - 您处理此问题的方式不正确。 Spring's @RestController converts incoming payload from JSON to the object parameter of the controller method by default and converts return object to JSON. Spring's @RestController incoming payload from JSON to the object parameter of the controller method by default and converts return object to JSON.

You don't need to include any extra dependencies apart spring-boot-starter-web , which includes a default Jackson JSON mapper.除了spring-boot-starter-web之外,您不需要包含任何额外的依赖项,其中包括默认的 Jackson JSON 映射器。

But even if you do use gson instead, Spring still will work this exact way only it'll use gson instead.但是,即使您确实使用gson代替, Spring 仍然会以这种方式工作,只是它会使用gson代替。

Everything you need to do is to create a class, which represents your incoming json correctly and use that class as a parameter, it has to have an empty constructor, setters if you use this object as a request body and getters if you use that object as a response body. Everything you need to do is to create a class, which represents your incoming json correctly and use that class as a parameter, it has to have an empty constructor, setters if you use this object as a request body and getters if you use that object作为响应体。

UPD: If you don't own the object you are using as a controller method's parameter - I recommend creating custom POJO anyway, using non-incapsulated third-party library considered a smell at least, it violates the encapsulation and creates tight coupling to the third-party library. UPD:如果您不拥有 object,则您将其用作 controller 方法的参数 - 我建议无论如何创建自定义 POJO,至少使用被认为是一种气味的非封装第三方库,它违反了封装并与第三方库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM