0

I`m trying to do post request:

public StudioResultDto createStudio(@RequestBody StudioDto studioDto) {
        var studioResultDto = new StudioResultDto();
        BeanUtils.copyProperties(studioDto, studioResultDto);
        studioResultDto.setId(registrationService.register(convertToEntity(studioDto)));
        return studioResultDto;
    }

Here is my Studio.java class:

package com.spdu.haircutstudio.registration.web.dto;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalTime;

public class StudioDto {
    private String name;
    private String phone;
    private String url;
    @JsonFormat(pattern = "hh:mm:ss")
    private LocalTime startTime;
    @JsonFormat(pattern = "hh:mm:ss")
    private LocalTime endTime;

    public StudioDto() {
    }

    public LocalTime getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalTime endTime) {
        this.endTime = endTime;
    }

    public LocalTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalTime startTime) {
        this.startTime = startTime;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
} 

But after doing request I got warning. Jackson because of some reason can't deserialize request body to my dto. Why does it happened? Body of request is:

{
  "endTime": {
    "hour": "0",
    "minute": "0",
    "nano": 0,
    "second": "0"
  },
  "name": "string",
  "phone": "string",
  "startTime": {
    "hour": "0",
    "minute": "0",
    "nano": 0,
    "second": "0"
  },
  "url": "string"
}

I have no response from the server. There is showing only 400 error. I tried to debug it, but I can't trace it in the method.

qli
  • 1
  • 3
  • Does [this](https://stackoverflow.com/questions/27952472/serialize-deserialize-java-8-java-time-with-jackson-json-mapper) help? Sounds like you are missing the datetime module of jackson. – Chaosfire Apr 19 '22 at 19:59
  • @Chaosfire I tried jsr10 from this list. This module didn't help. – qli Apr 19 '22 at 20:10

1 Answers1

0

Look at the pattern

@JsonFormat(pattern = "hh:mm:ss")
private LocalTime startTime;
@JsonFormat(pattern = "hh:mm:ss")
private LocalTime endTime;

Those patterns are strings, not objects, and you don't have nano seconds.

So, these parts of your request body

 {
    "hour": "0",
    "minute": "0",
    "nano": 0,
    "second": "0"
  }

Should probably look like this

"startTime": "00:00:00",
"endTime": "00:00:00",

I have no response from the server. There is showing only 404 error. I tried to debug it, but I can't trace it in the method.

Write a unit-test for your JSON parser, don't rely on the server to tell you if your code works.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I changed my body to ```{ "endTime": "00:00:00", "name": "string", "phone": "string", "startTime": "00:00:00", "url": "string" }``` got the same error. If i remove this annotations, how should my body looks like to be deserealized? – qli Apr 19 '22 at 20:07
  • You shouldnt remove any annotations. Do you have this module as a dependency? https://github.com/FasterXML/jackson-modules-java8 – OneCricketeer Apr 19 '22 at 20:08
  • And did you register it? https://github.com/FasterXML/jackson-modules-java8/tree/2.14/datetime#registering-module – OneCricketeer Apr 19 '22 at 20:09
  • So i add ```implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.13.2'``` and ```implementation 'com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.2'``` to my project. And pasted ```ObjectMapper mapper = JsonMapper.builder() .findAndAddModules() .build(); ``` to static void main for example and nothing changed. 400 request code. – qli Apr 19 '22 at 20:31
  • I have to tell, that im using swagger to test endpoints and this dependency autogenerating good request body in most cases. In this case body generated by swagger for my field look like: ```"endTime": { "hour": "string", "minute": "string", "nano": 0, "second": "string" }``` I changed ```"string'``` to ```"0"``` but still no works – qli Apr 19 '22 at 20:36
  • I don't know what your swagger definition looks like, but sounds to me like you need to create a custom object type with those four fields rather than LocalTime. You can use openapi-generator to create models from the openapi definition. Also not clear to me why any of those values would be strings, either. In any case, you can't use `@JsonFormat` for that. – OneCricketeer Apr 20 '22 at 13:58