0

I have a simple API which handles Entity objects (shown below).

@Data
public class Entity {

  private LocalDateTime createdAt;

}

A controller which looks like the following:

@RestController
@AllArgsConstructor
class MyController {

  private final EntityService entityService;

  @GetMapping
  Flux<Entity> getEntities() {
    return entityService.all(); // returns all entities from some storage location...
  }

}

When I make a GET request to my API, I receive the following response:

{
  "createdAt": "2022-07-10T20:39:01.147915"
}

Now, my API is designed to be consumed from a different origin, which means I need to add some custom CORS config to my application.

For this, I have created the following:

@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
        .allowedMethods("GET", "POST")
        .allowedOrigins("http://localhost:3000");
  }
}

After adding this and changing nothing else in my API the response changes to:

{
  "createdAt": [
    2022,
    7,
    10,
    20,
    39,
    1,
    147915000
  ]
}

Does anyone know what is causing this behaviour? Thanks

DGrinbergs
  • 27
  • 1
  • 5
  • Does this answer your question? [Java LocalDateTime being converted into an array of ints when being converted to JSON using Spring boot rest](https://stackoverflow.com/questions/61659191/java-localdatetime-being-converted-into-an-array-of-ints-when-being-converted-to) – Roman Jul 11 '22 at 05:26
  • Because you disabled the auto-configuration and thus it fallback to the basic defaults. Remove the `@EnableWebFlux` and Spring Boot will configure Jackson to use a timestamp (or pattern). – M. Deinum Jul 11 '22 at 05:58

1 Answers1

0

Try to put this annotation on your 'createdAt' field

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

And check out this question

Roman
  • 181
  • 7
  • That did it, and it turns out simply having `shape = JsonFormat.Shape.STRING` was enough to restore the original behaviour – DGrinbergs Jul 11 '22 at 10:56