Created
January 15, 2024 22:11
-
-
Save giovannicandido/b21964510d494598c0b5f4d1eb920d6d to your computer and use it in GitHub Desktop.
Configuring Spring Boot 3 Dates globally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.jackson.serialization.write-dates-as-timestamps=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
date=2024-01-15 | |
dateTime=2024-01-15T22:09:06.002Z | |
# DateTime precisa de ser enviado corretamente (escape de caracteres especiais) via postman e insomnia use a aba de parameters para fazer. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
public class ExampleController { | |
@GetMapping("/search") // esse metodo vai ser ativado se um GET for enviado para /clientes/search | |
public List<ClienteResponse> filtrarPeloNome(@RequestParam(required = false, name = "nomeCliente") String name, // parametro a ser passado na url | |
@RequestParam(required = false) Integer idade, | |
@RequestParam(required = false) List<Integer> ids, | |
@RequestParam(required = false) LocalDateTime dateTime, | |
@RequestParam(required = false) LocalDate date | |
) { | |
ClienteSearchParams searchParams = new ClienteSearchParams(idade, name, ids); | |
return clienteService.filtrar(searchParams) | |
.stream() | |
.map(ClienteResponse::of) | |
.toList(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.fundatec.spring.aula.config; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.format.FormatterRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
import org.springframework.core.convert.converter.Converter; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
@Configuration | |
public class WebMvcConfig implements WebMvcConfigurer { | |
@Override | |
public void addFormatters(FormatterRegistry registry) { | |
registry.addConverter(new Converter<String, LocalDateTime>() { | |
@Override | |
public LocalDateTime convert(String source) { | |
return LocalDateTime.parse(source, DateTimeFormatter.ISO_DATE_TIME); | |
} | |
}); | |
registry.addConverter(new Converter<String, LocalDate>() { | |
@Override | |
public LocalDate convert(String source) { | |
return LocalDate.parse(source, DateTimeFormatter.ISO_DATE); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment