设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 重新 试卷 文件
当前位置: 首页 > 综合聚焦 > 编程要点 > 正文

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

发布时间:2021-11-17 16:12 所属栏目:13 来源:互联网
导读:目录 json框架介绍,Jackson返回结果处理1、常用框架 阿里 fastjson,谷歌gson等2、jackson处理相关自动使用jackson返回json数据1、SpringMVC如何返回json数据2、例子 json框架介绍,Jackson返回结果处理 介绍常用json框架和注解的使用,自定义返回json结构和
目录
json框架介绍,Jackson返回结果处理1、常用框架 阿里 fastjson,谷歌gson等2、jackson处理相关自动使用jackson返回json数据1、SpringMVC如何返回json数据2、例子
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包
 
 <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
  </dependency>
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
 <mvc:annotation-driven/>
1.3、方法上添加@ResponseBody
 
 @ResponseBody
2、例子
2.1、配置pom.xml
 
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- Spring的核心工具包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!--在基础IOC功能上提供扩展服务,还提供许多企业级服务的支持,有邮件服务、任务调度、远程访问、缓存以及多种视图层框架的支持-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- Spring IOC的基础实现,包含访问配置文件、创建和管理bean等 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- Spring context的扩展支持,用于MVC方面 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- Spring表达式语言 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!--springAop开发必须加入的包-->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!--jackson返回json-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
  </dependencies>
2.2、配置spring.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫描controller-->
    <context:component-scan base-package="com"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--jsp所在位置-->
        <property name="prefix" value="/"></property>
        <!--jsp文件后缀名-->
        <property name="suffix" value=".jsp"></property>
    </bean>
    <mvc:annotation-driven /> <!--注解驱动-->
</beans>

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读