@DateTimeFormat 在发送响应时不转换日期

问题描述 投票:0回答:1

我有一个 Spring MVC 应用程序(无需启动)。我正在使用 Spring MVC 6。 我正在尝试学习 @ResponseBody 标签,并在控制器中创建了我的 bean 类的对象并将其在响应中发送。 日期元素已使用 @DateTimeFormat 注释进行注释,但是在发送响应时,我没有以特定格式获取它。

用户.java:

package com.habib.beans;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class User {
    private int id;
    private String name;
    private int age;
    private double marks;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private Date dob;
    private Address address;

    public User(int id, String name, int age, double marks, Date dob, Address address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.marks = marks;
        this.dob = dob;
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", marks=" + marks +
                ", dob=" + dob.toLocaleString() +
                ", address=" + address +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getMarks() {
        return marks;
    }

    public void setMarks(double marks) {
        this.marks = marks;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

FirstController.java

package com.habib.controllers;

import com.habib.beans.Address;
import com.habib.beans.City;
import com.habib.beans.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.GregorianCalendar;

@Controller
public class FirstController {
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public User first(ModelMap map) {

        User user =  new User(101,"Ali Asger Habib", 24, 98.54,
                new GregorianCalendar(2000, GregorianCalendar.MAY, 7).getTime(),
                new Address(23, "Fatehpura", new City("Udaipur", "Rajasthan", "India"), 313001));

        System.out.println(user);
        map.addAttribute("user", user);
        return user;
    }
}

我收到以下回复:

{
"id": 101,
"name": "Ali Asger Habib",
"age": 24,
"marks": 98.54,
"dob": 957637800000,
"address": {
"houseNo": 23,
"location": "Fatehpura",
"city": {
"cityName": "Udaipur",
"state": "Rajasthan",
"country": "India"
},
"pincode": 313001
}
}

我不知道为什么 DOB 没有以特定的格式出现。 我知道使用 @JsonFormat 可以解决我的问题,但我想知道为什么 @DateTimeFormat 在这种情况下不起作用。 另外,当我尝试在 JSP 中使用视图页面时,我仍然看不到它的定义格式,而是 Date 类的默认格式。 @DateTimeFormat 仅在解析请求时起作用还是双向起作用。

java spring spring-mvc datetime-format spring-annotations
1个回答
0
投票

JSON 支持的属性类型有字符串、数字、整数、对象、数组、布尔值和 null。它不支持日期类型。所以在 JSON 中。因此,Spring 会将日期类型转换为毫秒并发送 JSON 响应。 在本例中,出生日期为 2000-05-07 00:00:00 IST,自纪元时间起为 957637800000 毫秒。

© www.soinside.com 2019 - 2024. All rights reserved.