Springboot 添加fastjson依赖包

Springboot 添加fastjson依赖包

创建springboot项目

pom配置


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.dahai.springbootgroupId>
    <artifactId>springbootartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.8.RELEASEversion>
    parent>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.7version>
        dependency>



    dependencies>

project>

编写启动类

package com.dahai.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApp {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
        System.out.println("启动成功");
    }
}

创建controller

package com.dahai.springboot.controller;

import com.dahai.springboot.Demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  描述: @RestController等同于 @Controller + @ResponseBody
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {

        return "hello";
    }

    /**
     * 默认使用json返回数据
     * @return json
     */
    @GetMapping("getDemo")
    public Demo getDemo() {
        Demo demo = new Demo();
        demo.setId(1);
        demo.setName("tom");

        return demo;
    }
}

1.导入fastjson的依赖
2.将fastjson添加到转换器中

    // 将fastjson添加到转换器中
    @Override
    public void configureMessageConverters(List> converters) {
        super.configureMessageConverters(converters);

        /*
         * 1.定义一个convert消息转换对象
         * 2.添加fastJson配置信息,如:是否需要格式化返回的json数据
         * 3.在convert中添加配置信息
         * 4.将convert添加到converters当中
         */
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        converters.add(fastConverter);
    }

或者在启动类中使用一个@bean注解

    // 使用一个@bean注解
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        HttpMessageConverter converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

在要转换的地方加上注解 如:

package com.dahai.springboot;

import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;

public class Demo {
    private int id;
    private String name;
    @JSONField(format = "yyyy-MM-hh HH:mm")
    Date createDate;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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;
    }
}

返回的效果就是这样的

{
  "createDate": "2017-11-03 15:10",
  "id": 1,
  "name": "tom"
}

过滤字段

    // 不会返回该字段
    @JSONField(serialize = false)
    private String remade;

你可能感兴趣的:(springboot)