基于常用json框架介绍和Jackson返回结果处理方式

json框架介绍,Jackson返回结果处理

介绍常用json框架和注解的使用,自定义返回json结构和格式

1、常用框架 阿里 fastjson,谷歌gson等

JavaBean序列化为Json,性能:Jackson > FastJson > Gson > Json-lib 同个结构

Jackson、FastJson、Gson类库各有优点,各有自己的专长

空间换时间,时间换空间

2、jackson处理相关自动

  • 指定字段不返回:@JsonIgnore
  • 指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
  • 空字段不返回:@JsonInclude(Include.NON_NUll)
  • 指定别名:@JsonProperty

实体类代码如下:

public class rData {
    
    @JsonIgnore
    private String code;
    @JsonProperty(value = "agenum")
    private int age;
    @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
    private Date createDate;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    
    public rData(String code, int age) {
        this.code = code;
        this.age = age;
    }
    public rData(String code, int age, Date createDate, String name) {
        this.code = code;
        this.age = age;
        this.createDate = createDate;
        this.name = name;
    }
}

测试类代码:

@RestController
public class HttpController {
    @GetMapping("/testjson")
    public Object param6(){
        return new rData("jackson",1,new Date(),"lion");
    }
}

结果:

{"createDate":"2018-09-18 09:36:31","name":"lion","agenum":1}

code被忽略了,所以不显示;age被别名代替"agenum"

使用jackson返回json数据

1、SpringMVC如何返回json数据

1.1、添加jar包

 
      com.fasterxml.jackson.core
      jackson-databind
      2.9.5
  

1.2、配置spring文件,添加mvc命名空间和约束等

 xmlns:mvc="http://www.springframework.org/schema/mvc"
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd
 

1.3、方法上添加@ResponseBody

 @ResponseBody

2、例子

2.1、配置pom.xml

 
    
      junit
      junit
      4.11
      test
    
    
      org.springframework
      spring-webmvc
      5.0.8.RELEASE
    
    
      org.springframework
      spring-web
      5.0.8.RELEASE
    
    
    
      org.springframework
      spring-core
      5.0.8.RELEASE
    
    
    
      org.springframework
      spring-context
      5.0.8.RELEASE
    
    
    
      org.springframework
      spring-beans
      5.0.8.RELEASE
    
    
    
      org.springframework
      spring-context-support
      5.0.8.RELEASE
    
    
    
      org.springframework
      spring-expression
      5.0.8.RELEASE
    
    
    
      aopalliance
      aopalliance
      1.0
    
    
      org.aspectj
      aspectjweaver
      1.8.13
    
    
      org.springframework
      spring-aspects
      5.0.8.RELEASE
    
    
      org.springframework
      spring-aop
      5.0.8.RELEASE
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.5
    
  

2.2、配置spring.xml



    
    
    
    
        
        
        
        
    
     

2.3、配置web.xml



  Archetype Created Web Application
  
    springMvc
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring.xml
    
  
  
    springMvc
    /
  
  
  
    characterEncodingFilter
    
      org.springframework.web.filter.CharacterEncodingFilter
    
    
      encoding
      utf-8
    
  
  
    characterEncodingFilter
    /*
  

2.4、添加User实体类

package com.fan.entity;
import java.util.Arrays;
import java.util.Date;
public class User {
    private String username;
    private String password;
    private int[] box;
    private Date date;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int[] getBox() {
        return box;
    }
    public void setBox(int[] box) {
        this.box = box;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", box=" + Arrays.toString(box) +
                ", date=" + date +
                '}';
    }
}

2.5、 TestJsonController测试类

@Controller
public class TestJsonController {
     //http://localhost:8080/testjson1
    @RequestMapping("/testjson1")
    @ResponseBody //返回json字符串注解
    public User test(){
        User user=new User();
        user.setUsername("malijuan");
        user.setPassword("123");
        user.setBox(new int[]{1,2,3});
        user.setDate(new Date());
        return user;
    }
    //http://localhost:8080/testjson2
    @RequestMapping("/testjson2")
    @ResponseBody  //返回json字符串注解
    public List test2(){
        User user=new User();
        List list=new ArrayList();
        for(int i=0;i<5;i++){
            user.setUsername("malijuan");
            user.setPassword("123");
            user.setBox(new int[]{1,2,3});
            user.setDate(new Date());
            list.add(user);
        }
        return list;
    }
}

2.6、启动tomcat测试

基于常用json框架介绍和Jackson返回结果处理方式_第1张图片

测试1

测试2

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(基于常用json框架介绍和Jackson返回结果处理方式)