0

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>
Avv
  • 555
  • 1
  • 10
  • 18

1 Answers1

3

You have to annotate the field @field:JsonSerialize and not the property. Beside this you have to use @JsonFormat

@JacksonXmlRootElement
data class User(
    var email: String? = "",
    var lname: String = "",
    var fname: String = "",

    @field:JsonFormat(pattern = "yyyy-MM-dd")
    @field:JsonSerialize(using = LocalDateSerializer::class)
    @field:JsonDeserialize(using = LocalDateDeserializer::class)
    var birthday: LocalDate? = null
)

My small test:

val mapper = XmlMapper()
val xml = mapper.writeValueAsString(User(birthday = LocalDate.now()))
println(xml)

Generates the following output:

<User><email></email><lname></lname><fname></fname><birthday>2018-10-19</birthday></User>
Rene
  • 5,730
  • 17
  • 20
  • Added @field:JsonSerialize(using = LocalDateSerializer::class) and it works. Thanks. – Avv Oct 22 '18 at 05:21