從零開始學springboot筆記(二)-Spring boot返回json數據(中文無亂碼)


先創建json實體類,如下:

public class Demo {

    private int age;
    
    private String address;
    
    private String name;
    
    private String remark;
    
    private Date createTime;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
}

一:使用jackson返回json數據,具體如下:

說明:spring boot默認的json解析框架是jsckson解析,所以不需要添加任何依賴;

代碼如下:

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        
        return "hello";
    }
    
    @RequestMapping("/getDemo")
    public Demo getDemo(){
        
        Demo demo = new Demo();
        demo.setAddress("誰登錄看風景");
        demo.setAge(11);
        demo.setCreateTime(new Date());
        return demo;
    }
}

直接訪問:

二:使用fastjson

需要添加依賴:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

這里要說下很重要的話,官方文檔說的1.2.10以后,會有兩個方法支持HttpMessageconvert,一個是FastJsonHttpMessageConverter,支持4.2以下的版本,一個是FastJsonHttpMessageConverter4支持4.2以上的版本,
具體有什么區別暫時沒有深入研究。這里也就是說:低版本的就不支持了,所以這里最低要求就是1.2.10+。

 配置fastjon(支持兩種方法)

第一種:

(1)啟動類繼承extends WebMvcConfigurerAdapter
(2)覆蓋方法configureMessageConverters

代碼如下:

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter 
{
    public static void main( String[] args )
    {
        System.out.println( "--------------開始啟動---------------!" );
        SpringApplication.run(App.class, args);
        System.out.println( "--------------啟動成功---------------!" );
    }
    
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // TODO Auto-generated method stub
        super.configureMessageConverters(converters);
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );
//處理中文亂碼問題 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter); } }

重啟,然后訪問:

 第二種:在App.java啟動類中,注入Bean : HttpMessageConverters,代碼如下:

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter 
{
    public static void main( String[] args )
    {
        System.out.println( "--------------開始啟動---------------!" );
        SpringApplication.run(App.class, args);
        System.out.println( "--------------啟動成功---------------!" );
    }
        
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        
        //處理中文亂碼問題
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

}

注意:包別導錯了,是:import org.springframework.http.MediaType;

訪問后的結果和第一種一樣;

第一種和第二種都可以使用,自己根據個人喜好選擇;

使用了fastjson框架后,還可以使用fastjson的注解對返回的json數據進行特殊處理,如下:

@JSONField(format="yyyy-MM-dd HH:mm:ss")//格式化返回的日期
private Date createTime;

@JSONField(serialize=false)//是否需要序列化屬性,如果為false,那么將不會返回該數據信息
private String remark;

其他特性自己有興趣研究;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM