简体   繁体   English

Spring Boot杰克逊java.time.Duration

[英]Spring boot jackson java.time.Duration

I have create a restful web service and i try to send post request the above class 我已经创建了一个宁静的Web服务,并且尝试发送上述课程的发帖请求

public class A {
    Duration availTime;
}

public class MyRest {
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public Response<?> test(@RequestBody A ta) {
        return new ResponseEntity<>(HttpStatus.OK);`
    }
}

raw request body 原始请求主体

{
  "availTime": {
    "seconds": 5400,
    "nano": 0,
    "units": [
      "SECONDS",
      "NANOS"
    ]
  }
}

expected json result 预期的json结果

 {"availTime": "PT1H30M"}

and i got the above error : Failed to read HTTP message: 我收到了以上错误:无法读取HTTP消息:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected token (START_OBJECT),
expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.Duration value; nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected one of [VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT] for java.time.Duration value

As exception says java.time.Duration can be created only from int , float or string but you are providing a Json object. 异常说java.time.Duration只能从intfloatstring创建,但是您提供的是Json对象。

Define your Duration class as 将您的Duration类定义为

public class Duration {

    private long seconds;
    private long nano;
    private List<String> units;

    // setters / getters
}

Now your request will map from class A 现在您的请求将从A类映射

Edit 1 编辑1
In case you still want to map to java.time.Duration , then you need to change your request body. 如果仍然要映射到java.time.Duration ,则需要更改请求正文。
Case 1 (int) 案例1(int)

  • Request Body 请求正文

    { "availTime": 5400 } {“ availTime”:5400}

  • Output 产量

    {"availTime":"PT1H30M"} { “availTime”: “PT1H30M”}

Case 2 (float) 案例2(浮动)

  • Request Body 请求正文

    { "availTime": 5400.00 } {“ availTime”:5400.00}

  • Output 产量

    {"availTime":"PT1H30M"} { “availTime”: “PT1H30M”}

Case 3 (string) 情况3(字串)

  • Request Body 请求正文

    { "availTime": "PT5400S" } {“ availTime”:“ PT5400S”}

  • Output 产量

    {"availTime":"PT1H30M"} { “availTime”: “PT1H30M”}

For all above three cases to work add dependency 对于上述所有三种情况,添加依赖项

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
  <version>2.8.8</version>
</dependency>

Class A should be modified as A类应修改为

public class A {

    private Duration availTime;

    // for json output of duration as string 
    @JsonFormat(shape = Shape.STRING)
    public Duration getAvailTime() {
        return availTime;
    }

    public void setAvailTime(Duration availTime) {
        this.availTime = availTime;
    }
}

To Print request body as json 将请求正文打印为json

ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
System.out.println(mapper.writeValueAsString(ta));

If you still want to map your existing request body, then you need to write your custom deserializer for Duration class. 如果仍然要映射现有的请求主体,则需要为Duration类编写自定义反序列化器。

Hope it helps. 希望能帮助到你。

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

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