springMVC返回json数据

创建一个User对象,将User对象转换为json 字符串格式返回

package controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.User;
import java.util.ArrayList;

@Controller
public class UserController {
    //json返回一个字符串,第三方jar包可以实现,该jar包jackson
    
    //produces解决编码乱码问题
    @RequestMapping(value = "/json1")
    //将服务器端返回对象转换为json对象响应回去
    @ResponseBody
    public String json1() throws JsonProcessingException {
        //需要一个jackson对象,就是一个类,将对象转换字符串
        ObjectMapper objectMapper = new ObjectMapper();

        //创建一个对象
        User user = new User("小敏",3,"女");

        //将java对象转换为json字符串
        String str = objectMapper.writeValueAsString(user);

        //加了responseBody注解,这里会将str以json格式字符串返回
        return str;
    }
    
      //返回多个对象
    @RequestMapping("/json12")
    @ResponseBody
    public String json12() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();

        ArrayList<User> users = new ArrayList<>();

        User user=new User("小明",3,"男");
        User user1 = new User("小红", 4, "女");
        users.add(user);
        users.add(user1);

        String string = objectMapper.writeValueAsString(users);

        return string;

    }

}

将日期转为json字符串返回

package controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;


@Controller
public class DateController {

    @RequestMapping("/date1")
    @ResponseBody
    public String DateJson() throws JsonProcessingException {
        Date date = new Date();

        //返回的是时间戳
        return new ObjectMapper().writeValueAsString(date);
    }

    //解决返回时间戳的问题,返回自定义字符串格式
    @RequestMapping("/date2")
    @ResponseBody
    public String DateJson2() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();

        //设置时间不返回时间戳
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

        //自定义时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //让objectMapper指定为日期时间格式
        objectMapper.setDateFormat(simpleDateFormat);

        Date date = new Date();

        return objectMapper.writeValueAsString(date);
    }
}

在此我们会发现每次使用都会出现相同代码,我们可以把相同代码封装成一个工具类,后面使用调用该方法即可

创建一个JsonUntil工具类

package until;

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

import java.text.SimpleDateFormat;

public class JsonUntil {

    public static String getJson(Object object){
        return getJson(object,"yyyy-MM-dd HH:mm:ss");
    }

    public static String getJson(Object object,String dateFormat){
        ObjectMapper mapper=new ObjectMapper();

        //让时间不返回时间戳
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

        //自定义日期格式对象
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(dateFormat);

        //让mapper指定为时间日期格式
        mapper.setDateFormat(simpleDateFormat);

        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

将对象转换为接送字符串代码可以简化为:

@RequestMapping("/json1")
@ResponseBody
public String json1(){
    User user = new User("小敏",3,"女");
    return JsonUntil.getJson(user);
}

日期转换为json字符串对象代码可以简化为:

@RequestMapping(value = "/date2")
@ResponseBody
public String Datejson2(){
    //写一个日期对象
    Date date = new Date();

    return JsonUntil.getJson(date);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swttws.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值