方法重载实现复用

Controller类
UserController.java

package com.li.Controller;

import com.li.Utils.UserUtils;
import com.li.pojo.User;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Controller
@ResponseBody
public class UserController {
    /*
    解决乱码方案一
    @RequestMapping(value = "/json1",produces = "application/json;charset=utf-8")
    */
    @RequestMapping("/json1")
    public String test1(){
        //定义数据
        User user1 = new User(001,"小李","男");
        User user2 = new User(002,"小张","女");
        User user3 = new User(003,"小王","男");

        //创建一个User类型的数组用来存放数据
        List<User> UserList = new ArrayList<>();
        //添加数据
        UserList.add(user1);
        UserList.add(user2);
        UserList.add(user3);
        //调用工具类,创建JsonMapper对象,并调用他的.writeValueAsString方法用来转化数组为Json对象
        UserUtils userUtils = new UserUtils();
        String json = userUtils.getJson(UserList);
        return json;
    }
    @RequestMapping("/json2")
    public String test2(){
        //定义数据
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        return format;
    }
    @RequestMapping("/json3")
    public String test3(){
        //定义数据
        Date date = new Date();

        //调用工具类,创建JsonMapper对象,并调用他的.writeValueAsString方法用来转化数组为Json对象
        UserUtils userUtils = new UserUtils();
        String json = userUtils.getJson(date,"YYYY-MM-dd HH:mm:ss");
        return json;
    }
}

方法工具类
UserUtils.java

package com.li.Utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.li.pojo.User;

import java.text.SimpleDateFormat;

public class UserUtils {

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

    public String getJson(Object object,String sdf){
        String str = "";
        //实例化JsonMapper对象
        JsonMapper jsonMapper = new JsonMapper();
        //默认为使用时间戳方式,这里设置false为不使用时间戳方式
        jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //根据传的值设置日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(sdf);
        jsonMapper.setDateFormat(simpleDateFormat);
        try {
            str = jsonMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
}

请求localhost:8080/json1或者请求localhost:8080/json3调用为同一个方法

根据方法重载实现复用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值