I create a POST API in spring boot + kotlin. I create a LocalDate object but response is something not desired. I have used
org.springframework.format.annotation.DateTimeFormat
What I get is something like:
<user>
<email>a@mail.com</email>
<lname></lname>
<fname></fname>
<birthday>
<year>2000</year>
<month>JANUARY</month>
<chronology>
<id>ISO</id>
<calendarType>iso8601</calendarType>
</chronology>
<dayOfMonth>1</dayOfMonth>
<dayOfWeek>SATURDAY</dayOfWeek>
<era>CE</era>
<dayOfYear>1</dayOfYear>
<leapYear>true</leapYear>
<monthValue>1</monthValue>
</birthday>
</user>
What I want is something like (specifically birthDay tag):
<user>
<email>a@mail.com</email>
<lname></lname>
<fname></fname>
<birthday>2000-01-01</birthday>
</user>
Here is the code:
dto class:
import org.springframework.format.annotation.DateTimeFormat
import java.time.LocalDate
@JacksonXmlRootElement
data class User (var email: String? = "",
var lname: String = "",
var fname: String = "",
@DateTimeFormat(pattern = "yyyy-MM-dd")
var birthday: LocalDate? = null)
controller class:
@RestController
@RequestMapping("/")
class Controller {
@PostMapping("/post")
fun registerByMail(@Valid body: User) : ResponseEntity<Any> {
.
.
.
var user = User(birthDay = body.birthDay)
return ResponseEntity.ok(user)
Please let me know where I doing wrong. I am making POST request using postman.
Edit : I have also tried solution mentioned here : JSON Java 8 LocalDateTime format in Spring Boot but that also doesn't worked for me.
When I use com.fasterxml.jackson.annotation.JsonFormat annotation and required dependency, I got following error:
<defaultMessage>Failed to convert value of type 'java.lang.String[]' to
required type 'java.time.LocalDate'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type
[@com.fasterxml.jackson.annotation.JsonFormat java.time.LocalDate] for value
'2000-01-01'; nested exception is java.lang.IllegalArgumentException: Parse
attempt failed for value [2000-01-01]</defaultMessage>