1

I have an endpoint that accepts @ResponseBody.

But when I sent the data, I could not understand why the status 400 was coming to me in response. There was nothing in the logs that could tell me.

  • build.gradle

plugins {
    id 'org.springframework.boot' version '2.7.5'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

{
...
implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.fasterxml.jackson.core:jackson-annotations'
    implementation 'org.flywaydb:flyway-core'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
....
}

  • controller
 @PostMapping(value = "new")
    public ResponseEntity<TDto> addNew(@Valid @RequestBody TDto dto) {
...
}
  • application.yml
logging:
  level:
    org:
      springframework:
        web:
          filter:
            CommonsRequestLoggingFilter: DEBUG
          util:
            ContentCachingRequestWrapper: DEBUG
            ContentCachingResponseWrapper: DEBUG

management:
  endpoints:
    web:
      exposure:
        include: httptrace

I have the dto:

@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class TDto {

    private Long id;

.....
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonProperty("t_date")
    private LocalDateTime tDate;

It turned out that jackson can't convert a date in this format

"2022-10-22 10:17:37.402150 +00:00"

but it works with a date of this format

"2021-08-02T03:00:00"

To make it work, I had to do this:

    @JsonProperty("t_date")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime tDate;

and the date has to be transmitted strictly in this format"

"2021-09-15T10:15:37.93456"
or
"2021-09-17T10:14:37"

and how to set up different formats is not understood.

For example, I can't use time variables in Postman

  "t_date": {{$timestamp}},

since in this case, its own date format is generated (also timestamp).

But the worst thing is that I don't see any errors in the logs.

They just don't exist.

In addition, how in general can you configure several Jackson date serializers and specify that Jackson automatically selects the appropriate one, and if it does not find it, then throws the user exception?

This error was not in close visibility, how to enable logging of such problems?

skyho
  • 1,438
  • 2
  • 20
  • 47
  • Specify your date format on the date field attribute with ``@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")`` see https://www.baeldung.com/jackson-jsonformat – codiallo Oct 22 '22 at 15:47
  • It's doesnt'work. – skyho Oct 22 '22 at 16:46

1 Answers1

0

To use custom format you can use annotation like this:

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    private LocalDateTime tDate;

For more details see this question and its accepted answer: Spring Data JPA - ZonedDateTime format for json serialization

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • thank you . but after all, client applications can send dates in different formats, and I don't want to create a separate endpoint and dto for each such case. Moreover, when the date arrives in the wrong format, I will not be able to intercept the Exception and indicate it to the client, the client will only see the status = 400. In addition, I can't see the problem in Debug, since the exception occurs before entering endpoint. – skyho Oct 24 '22 at 11:41