一、pom.xml

    <dependencies>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.11.2</version>
    </dependency>
</dependencies>

二、返回对象形式的json字符串

controller:

    @ResponseBody //不走视图解析器 这个方法返回的是字符串
    @RequestMapping("/json1")
    //返回对象的json格式
    public String json1() throws JsonProcessingException {
        User user = new User("kexing",20,"男");
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(user);
        return s;
    }

输出:

{"name":"kexing","age":20,"sex":"男"}

在Controller中加上@RestController或着@ResponseBody可让方法不走视图解析器,直接返回一个字符串,@RestController作用在类上,该类的所有方法都返回的是字符串,@ResponseBody作用在方法上

三、返回集合形式的json字符串

controller:

    @ResponseBody //不走视图解析器 这个方法返回的是字符串
    @RequestMapping("/json2")
    //返回集合的json格式
    public String json2() throws JsonProcessingException {
        User user1 = new User("kexing1",20,"男");
        User user2 = new User("kexing2",20,"男");
        User user3 = new User("kexing3",20,"男");
        User user4 = new User("kexing4",20,"男");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(list);
        return s;
    }

输出:

[{"name":"kexing1","age":20,"sex":"男"},{"name":"kexing2","age":20,"sex":"男"},{"name":"kexing3","age":20,"sex":"男"},{"name":"kexing4","age":20,"sex":"男"}]

三、返回Date形式的json字符串

    @ResponseBody
    @RequestMapping("/json3")
    public String getDateForJson1() {
        try {
            return new ObjectMapper().writeValueAsString(new Date());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

ObjectMapper中默认返回的Date类型是时间戳,也就是距今为止的毫秒值

输出:

1596703261213

四、返回格式化Date后的json字符串

    @ResponseBody
    @RequestMapping("/json4")
    public String getDateForJson2() {
        try {
            return new ObjectMapper().writeValueAsString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

这是直接通过SimpleDateFormat进行格式化

输出:

"2020-08-06 16:42:31"

五、关闭ObjectMapper设置的时间戳,设置对应的DateFormat

    @ResponseBody
    @RequestMapping("/json5")
    public String getDateForJson3() {
        ObjectMapper mapper = new ObjectMapper();
        //关闭ObjectMapper默认设置的时间戳方式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        try {
           return mapper.writeValueAsString(new Date());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

输出:

"2020-08-06 16:44:34"

六、封装Util

package cn.kexing.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;

public class JsonUtil {
    //返回指定格式的时间对象Json格式
    public static String getJson(Object object, SimpleDateFormat sdf){
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        mapper.setDateFormat(sdf);
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    //返回指定对象的Json格式
    public static String getJson(Object object){
        return getJson(object,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }
}
最后修改:2021 年 11 月 15 日 05 : 22 PM
如果觉得我的文章对你有用,请随意赞赏