SpringBoot統合fastJson
5418 ワード
fastJsonはjava言語で作成された高性能機能の完備したjsonライブラリであり、アリのオープンソースプロジェクトであり、現在java言語の中で最も速いJSONライブラリであり、インタフェースは簡単で使いやすい.
一、pom.xmlファイルの追加依存ここで私が追加したのはfastJsonの最新バージョンで、バージョンは行くことができますhttps://mvnrepository.com/artifact/com.alibaba/fastjsonクエリー.
二、具体的に二つの方式を使用する:1.クラス継承WebMvcConfigurerAdapterを起動し、configureMessageConvertersメソッド2を書き換える.起動クラスにbean注入HttpMessageConvertersここで私が使っているのは1つ目です.
VoiceSourceModelエンティティークラス:
制御クラスIndexController:
1.クラス継承WebMvcConfigurerAdapterを起動し、configureMessageConvertersメソッドを書き換える
右クリックRun As->Javaアプリケーション、起動アプリケーション、ブラウザ入力http://localhost:6060/index/test_fastjsonアクセスは次のように成功を示します.
2.起動クラスのbean注入HttpMessageConverters
起動後の効果は次のとおりです.
なぜか木の構造ではない.効果については、最初の構成が好きなので、最初の構成を採用しました.
説明:第2の構成を使用すると、起動後にjsonが中国語の文字化けしに遭遇し、これを解決する方法はGetMappingにproduces="application/json;charset=utf-8"を追加し、この方法は同様に第1の構成で遭遇した中国語の文字化けしを解決することができ、以下に示す.
また、Springboot 2についても知っています.0以降、WebMvcConfigurerAdapterはSpringboot 2のため時代遅れになった.0最低サポートJDK 1.8,interfaceインタフェースに構成されたメソッドの場合,サブクラスは抽象クラス内で実装されたインタフェースメソッドに相当するメソッドを実装しなくてもよい新しいdefaultキーワードを導入した.新しい代替方法は、WebMvcConfigurerを直接実装するか、WebMvcConfigurationSupportを実装することです.デバッグで間違いがないことを確認したので、最初のものを以下のように修正しました.最終版:
一、pom.xmlファイルの追加依存ここで私が追加したのはfastJsonの最新バージョンで、バージョンは行くことができますhttps://mvnrepository.com/artifact/com.alibaba/fastjsonクエリー.
com.alibaba
fastjson
1.2.57
二、具体的に二つの方式を使用する:1.クラス継承WebMvcConfigurerAdapterを起動し、configureMessageConvertersメソッド2を書き換える.起動クラスにbean注入HttpMessageConvertersここで私が使っているのは1つ目です.
VoiceSourceModelエンティティークラス:
package com.hearing.model;
import lombok.Data;
/**
*
* Created by liulin on 2019/2/8.
*/
@Data
public class VoiceSourceModel {
/**
* id
*/
private int id;
/**
*
*/
private String voiceName;
/**
* url
*/
private String voiceUrl;
/**
* 1-mp3, 2-wma, 3-wave
*/
private int voiceType;
}
制御クラスIndexController:
@RestController
@RequestMapping("/index")
public class IndexController {
@GetMapping("test_fastjson")
public String testFastJson(HttpServletRequest request) {
VoiceSourceModel voiceSourceModel = new VoiceSourceModel();
voiceSourceModel.setId(1);
voiceSourceModel.setVoiceName(" ");
voiceSourceModel.setVoiceUrl("www.baidu.com");
voiceSourceModel.setVoiceType(3);
return JSON.toJSONString(voiceSourceModel);
}
}
1.クラス継承WebMvcConfigurerAdapterを起動し、configureMessageConvertersメソッドを書き換える
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HearingTestApplication extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters (List> converters) {
// converter
FastJsonHttpMessageConverter fastconverter = new FastJsonHttpMessageConverter();
// fastjson , : json
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// converter
fastconverter.setFastJsonConfig(fastJsonConfig);
// converter converters
converters.add(fastconverter);
}
public static void main(String[] args) {
SpringApplication.run(HearingTestApplication.class, args);
}
}
右クリックRun As->Javaアプリケーション、起動アプリケーション、ブラウザ入力http://localhost:6060/index/test_fastjsonアクセスは次のように成功を示します.
{
"id": 1,
"voiceName": " ",
"voiceType": 3,
"voiceUrl": "www.baidu.com"
}
2.起動クラスのbean注入HttpMessageConverters
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HearingTestApplication {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// converter
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// fastjson , : json
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// converter
fastConverter.setFastJsonConfig(fastJsonConfig);
// converter HttpMessageConverter
HttpMessageConverter> converter = fastConverter;
// HttpMessageConverters
return new HttpMessageConverters(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(HearingTestApplication.class, args);
}
}
起動後の効果は次のとおりです.
"{\"id\":1,\"voiceName\":\" \",\"voiceType\":3,\"voiceUrl\":\"www.baidu.com\"}"
なぜか木の構造ではない.効果については、最初の構成が好きなので、最初の構成を採用しました.
説明:第2の構成を使用すると、起動後にjsonが中国語の文字化けしに遭遇し、これを解決する方法はGetMappingにproduces="application/json;charset=utf-8"を追加し、この方法は同様に第1の構成で遭遇した中国語の文字化けしを解決することができ、以下に示す.
@GetMapping(value = "test_fastjson", produces = "application/json;charset=UTF-8”)
また、Springboot 2についても知っています.0以降、WebMvcConfigurerAdapterはSpringboot 2のため時代遅れになった.0最低サポートJDK 1.8,interfaceインタフェースに構成されたメソッドの場合,サブクラスは抽象クラス内で実装されたインタフェースメソッドに相当するメソッドを実装しなくてもよい新しいdefaultキーワードを導入した.新しい代替方法は、WebMvcConfigurerを直接実装するか、WebMvcConfigurationSupportを実装することです.デバッグで間違いがないことを確認したので、最初のものを以下のように修正しました.最終版:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HearingTestApplication implements WebMvcConfigurer {
@Override
public void configureMessageConverters (List> converters) {
// converter
FastJsonHttpMessageConverter fastconverter = new FastJsonHttpMessageConverter();
// fastjson , : json
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// converter
fastconverter.setFastJsonConfig(fastJsonConfig);
// converter converters
converters.add(fastconverter);
}
public static void main(String[] args) {
SpringApplication.run(HearingTestApplication.class, args);
}
}