栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springMVC使用JSON

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springMVC使用JSON

springMVC使用JSON时首先要导入架包

Jackson Databid

对于JSON乱码问题,可以在springmvc-servlet.xml中加入配置

    @RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")

或者


       
           
               
           
           
               
                   
                       
                   
               
           
       

    

案例:
方法一:

 @RequestMapping("/j2")
    // @ResponseBody
    public String json2() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        List userList = new ArrayList();
        User user1 = new User(1, "name", 13);
        User user2 = new User(2, "name", 13);
        User user3 = new User(3, "name", 13);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        String str = objectMapper.writevalueAsString(userList);
        return str;
    }
 @RequestMapping("/j3")
    // @ResponseBody
    public String json3() throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();
       //自定义日期格式
       objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(sdf);

        Date date = new Date();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String str=objectMapper.writevalueAsString(date);  
             return str;
       }```

```bash

方式二:为了方便,首先创建一个JSON工具类

package com.li.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 JsonUtils {
    public static String getJson(Object object){
        return getJson(object,"yyyy-MM-dd HH:mm:ss");
    }
    public static String getJson(Object object,String Format){
        ObjectMapper objectMapper=new ObjectMapper();
        //自定义日期格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat sdf=new SimpleDateFormat(Format);
        objectMapper.setDateFormat(sdf);
        try {
            return objectMapper.writevalueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return  null;
    }
}

调用;

 @RequestMapping("/j4")
    // @ResponseBody
    public String json4() throws JsonProcessingException {

        List userList = new ArrayList();
        User user1 = new User(1, "name", 13);
        User user2 = new User(2, "name", 13);
        User user3 = new User(3, "name", 13);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        return JsonUtils.getJson(userList);
    }
     @RequestMapping("/j5")
    // @ResponseBody
    public String json3() throws JsonProcessingException {
        Date date = new Date();
        return JsonUtils.getJson(date, "yyyy-MM-dd HH:mm:ss");
    }

除了自己写的json,还用一种fastjson可以使用
首先添加依赖,


        org.projectlombok
        lombok
        1.18.22
    

直接使用

 //使用fastJson
    @RequestMapping("/j5")
    // @ResponseBody
    public String json5() throws JsonProcessingException {

        List userList = new ArrayList();
        User user1 = new User(1, "name", 13);
        User user2 = new User(2, "name", 13);
        User user3 = new User(3, "name", 13);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
       return JSON.toJSONString(userList);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/532242.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号