格式化日期以响应POST rest API

格式化日期以响应POST rest API,rest,spring-boot,kotlin,Rest,Spring Boot,Kotlin,我在spring boot+kotlin中创建了一个POST API。我创建了一个LocalDate对象,但响应是不需要的。我用过 org.springframework.format.annotation.DateTimeFormat 我得到的是: <user> <email>a@mail.com</email> <lname></lname> <fname></fname> &

我在spring boot+kotlin中创建了一个POST API。我创建了一个LocalDate对象,但响应是不需要的。我用过

org.springframework.format.annotation.DateTimeFormat

我得到的是:

<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>
控制器类:

@RestController
@RequestMapping("/")
class Controller {


    @PostMapping("/post")
    fun registerByMail(@Valid body: User) : ResponseEntity<Any> {
    .
    .
    .
    var user = User(birthDay = body.birthDay)
    return ResponseEntity.ok(user)
@RestController
@请求映射(“/”)
类控制器{
@邮戳(“/post”)
有趣的registerByMail(@Valid body:User):ResponseEntity,但这对我也不起作用

当我使用com.fasterxml.jackson.annotation.JsonFormat注释和必需的依赖项时,我得到了以下错误:

<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>
未能将“java.lang.String[]”类型的值转换为
必需类型“java.time.LocalDate”;嵌套异常为
org.springframework.core.convert.ConversionFailedException:未能
从类型[java.lang.String]转换为类型
[@com.fasterxml.jackson.annotation.JsonFormat java.time.LocalDate]获取值
“2000-01-01”;嵌套异常为java.lang.IllegalArgumentException:Parse
尝试获取值[2000-01-01]失败

您必须注释字段
@field:JsonSerialize
,而不是属性。 除此之外,您还必须使用
@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
)
我的小测验:

val mapper = XmlMapper()
val xml = mapper.writeValueAsString(User(birthday = LocalDate.now()))
println(xml)
生成以下输出:

<User><email></email><lname></lname><fname></fname><birthday>2018-10-19</birthday></User>
2018-10-19

您必须注释字段
@field:JsonSerialize
,而不是属性。 除此之外,您还必须使用
@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
)
我的小测验:

val mapper = XmlMapper()
val xml = mapper.writeValueAsString(User(birthday = LocalDate.now()))
println(xml)
生成以下输出:

<User><email></email><lname></lname><fname></fname><birthday>2018-10-19</birthday></User>
2018-10-19

我尝试了此解决方案,但出现了错误。上面的内容已编辑。谢谢。我尝试了此解决方案,但出现了错误。上面的内容已编辑。谢谢。添加了@field:JsonSerialize(使用=LocalDateSerializer::class)并正常工作。谢谢。添加了@field:JsonSerialize(使用=LocalDateSerializer::class)并正常工作。谢谢。