json日期格式化与json日期转化为对象

项目基础

SpringBoot+web+fastjson

问题1提出:返回JSON对象的日期需要格式化

测试代码

对象

@Data
@Accessors(chain = true)
public class Student {
    private Integer id;
    private String name;
    private Date date;
}

接口 

@RestController
public class StudentController {

    @RequestMapping("/students")
    public List<Student> hello() {

        List<Student> students = new ArrayList<>();
        Student student = new Student();
        student.setId(1).setName("1").setDate(new Date());
        Student student2 = new Student();
        student2.setId(2).setName("2").setDate(new Date());

        students.add(student);
        students.add(student2);

        return students;
    }
}

显示结果

解决办法

添加依赖

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0</version>
            <scope>compile</scope>
        </dependency>

在需要格式的属性上修改如下

@JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date date;

添加配置类

 @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

运行结果如下: 

参考:https://blog.csdn.net/qq_28929589/article/details/79245774

问题2提出:把带有日期格式的json字符串变为对象

对象字符串

{ "date":"2020-01-08 22:59:48", "id":1, "name":"1" }

测试代码

使用下面里面的一个工具类

https://blog.csdn.net/qq_37171353/article/details/103848712

@RequestMapping("/hello")
    public String str2obj() {
        //{ "date":"2020-01-08 22:59:48", "id":1, "name":"1" }
        String str = "{ \"date\":\"2020-01-08 22:41:54\", \"id\":1, \"name\":\"1\" }";
        //https://blog.csdn.net/qq_37171353/article/details/103848712
        //里的json工具类
        Student student = JsonUtils.jsonToPojo(str, Student.class);
        System.out.println(student);

        return "success";
    }

 出现异常

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-01-08 22:41:54": not a valid representation (error: Failed to parse Date value '2020-01-08 22:41:54': Cannot parse date "2020-01-08 22:41:54": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
 at [Source: (String)"{ "date":"2020-01-08 22:41:54", "id":1, "name":"1" }"; line: 1, column: 10] (through reference chain: com.example.json2date.entity.Student["date"])

 解决办法

修改实体类如下

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    private Date date;
发布了99 篇原创文章 · 获赞 115 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/qq_37171353/article/details/103899294