スプリングブートは、スプリングフレームワークに基づいています。 Spring Frameworkの機能の強化ではなく、Springをすばやく構築する方法です。
Spring-Bootアプリケーションは、JacksonのデフォルトのJSONコンバーターを提供します。例:
pom.xmlの依存関係構成:
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/maven-v4_0_0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.qinker </groupid> <artifactid> spring-boot </artifactid> <パッケージング> war </pack </groupid> <artifactid> spring-boot-starter-parent </artifactid> <bersion> 2.0.0.release </version> </parent> <version> 0.0.1-snapshot </version> <name> spring-boot> <url> http://maven.apache.org <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 9 </java.version> </properties> <dependencies> <! - https://mvnrepository.com/artifact/org. <GroupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> </dependencies> <burtion> <bultname> spring-boot </finalName> </build> </project>
MainApp.javaとuser.javaとhellocontroller.javaの3つのクラスを作成します:
パッケージcom.springboot; Import Java.util.date; org.springframework.web.bind.annotation.requestmappingをインポートします。 org.springframework.web.bind.annotation.restControllerをインポートします。 @RestController Public Class Hellocontroller {@RequestMapping( "/hello")public string hello(){return "hello、springboot"; }/** *スプリングブートデフォルトのJSON解像度フレームワークはJackson * @return */@RequestMapping( "/getUser")public user getUser(){user u = new user(); U.SetName( "Zhang San"); U.Setage(33); U.SetCreatetime(new Date()); uを返します。 }}パッケージcom.springboot; java.io.serializableをインポートします。 Import Java.util.date;パブリッククラスユーザーはSerializable {private string name;プライベートインクエイジ;プライベートデートCreateTime; public string getName(){return name; } public void setName(string name){this.name = name; } public int getage(){return age; } public void Setage(int age){this.age = age; } public date getCreatetime(){return createTime; } public void setCreatetime(date createTime){this.createtime = createTime; }}パッケージcom.springboot; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; @springBootApplication public class mainApp {public static void main(string [] args){springApplication.run(mainapp.class、args); }}MainAppを開始:http:// localhost:8080/getuserにアクセスしてください。結果は次のとおりです。
{"name": "Zhang San"、 "age":33、 "createTime": "2018-04-04t03:03:08.534+0000"}構成を作成しなかったことがわかりますが、JSONデータを返しました。 Spring-BootがJSONのデフォルトの実装を作成し、組み込みのJackson Converterを使用していることがわかります。
それでは、fastjsonを例として取り上げましょう。
まず、FastJsonパッケージを紹介し、POMに次の依存関係を追加します。
<! - https://mvnrepository.com/artifact/com.alibaba/fastjson-> <dependency> <groupid> com.alibaba </groupid> <artifactid> fastjson </artifactid> <バージョン
効果を容易にするには、ユーザークラスを変更します。
パッケージcom.springboot; java.io.serializableをインポートします。 Import Java.util.date; com.alibaba.fastjson.annotation.jsonfieldをインポートします。 @suppresswarnings( "serial")パブリッククラスユーザーはserializable {private string name;プライベートインクエイジ; @jsonfield(format = "yyyy-mm-dd hh:mm")private date createTime; public string getName(){return name; } public void setName(string name){this.name = name; } public int getage(){return age; } public void Setage(int age){this.age = age; } public date getCreatetime(){return createTime; } public void setCreatetime(date createTime){this.createtime = createTime; }}1. FastJSONカスタムJSON変換を実装する最初の方法、Spring-BootはWebMVCConVenterインターフェイスを実装します。
MainAppを次のように変更します。
パッケージcom.springboot; java.util.arraylistをインポートします。 java.util.listをインポートします。 Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; org.springframework.http.mediatypeをインポートします。 Import org.springframework.http.converter.httpmessageconverter; Import org.springframework.web.servlet.config.annotation.webmvcconfigurer; com.alibaba.fastjson.serializer.serializerfeatureをインポートします。 com.alibaba.fastjson.support.config.fastjsonconfigをインポートします。 com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverterをインポートします。 @SpringBootApplication Public Class MainAppを実装したWebmvcconfigurer {@Override public void configuremessageconverters(list <httpmessageconverter <?>> converters){webmvcconfigurer.super.configuremessageconverters(converters); // FastJSONコンバーターインスタンスFastJSONHTTPMESSAGECONVERTER converter = new FastJSONHTTPMESSAGECONVERTER(); // configuration object fastjsonconfig config = new fastjsonconfig();リスト<Mediatype> mediatypes = new ArrayList <>(); //中国のエンコーディングmediatype mediatype = mediatype.application_json_utf8; mediatypes.add(mediatype); config.setserializerfeatures(serializerfeature.prettyformat); converter.setsupportedmediatypes(mediatypes); converter.setfastjsonconfig(config); converters.add(converter); } public static void main(string [] args){springApplication.run(mainapp.class、args); }}プログラムの開始:上記のパスにアクセス:ブラウザには次の結果が表示されます。
{"age":33、 "createtime": "2018-04-04 11:14"、 "name": "Zhang San"} 2。@Beanアノテーションを使用してFastJSONコンバーターを注入します:MainAppを次のように変更します。
パッケージcom.springboot; java.util.arraylistをインポートします。 java.util.listをインポートします。 Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; Import org.springframework.boot.autoconfigure.http.httpmessageconverters; org.springframework.context.annotation.beanをインポートします。 org.springframework.http.mediatypeをインポートします。 com.alibaba.fastjson.serializer.serializerfeatureをインポートします。 com.alibaba.fastjson.support.config.fastjsonconfigをインポートします。 com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverterをインポートします。 @SpringBootApplication Public Class MainApp {@Bean public httpmessageconverters fastjsonhtpmessageconventers(){fastjsonhttpmessageconverter converter = new fastjsonhttpmessageconverter(); fastjsonconfig config = new fastjsonconfig(); config.setserializerfeatures(serializerfeature.prettyformat);リスト<Mediatype> mediatypes = new ArrayList <>(); mediatypes.add(mediatype.application_json_utf8); converter.setsupportedmediatypes(mediatypes);新しいhttpmessageconverters(converter)を返します。 } public static void main(string [] args){springApplication.run(mainapp.class、args); }}アクセス結果は同じです。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。