Spring-Boot basiert auf dem Frühlingsgerüst. Es ist keine Verbesserung der Funktionalität des Spring -Frameworks, sondern eine Möglichkeit, schnell den Frühling zu bauen.
Die Spring-Boot-Anwendung bietet den Standard-JSON-Konverter für Jackson. Beispiel:
Abhängigkeitskonfiguration in pom.xml:
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qinker</groupId> <artifactId>spring-boot</artifactId> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactid> Spring-Boot-Starter-Eltern </artifactId> <version> 2.0.0.Release </Version> </parent> <version> 0.0.1-Snapshot </Version> <name> Spring-Boot </name> <url> http <project.build.sourceencoding> utf-8 </project.build.SourceCoding> <java.version> 9 </java.version> </properties> <Depelences> <!-https://mvnrepository.com/artifact/Org.ssprung-> <elegring. <gruppe> org.springFramework.boot </gruppacid> <artifactId> Spring-Boot-Starter-Web </artifactId> </abhängig> </abhängig> <builds> <ElfinalName> Spring-Boot </Finalname> </build> </project> </project>
Erstellen Sie drei Klassen: mainapp.java und user.java und hellocontroller.java:
Paket com.springboot; import Java.util.date; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.restController; @RestController Public Class HelloConTroller {@RequestMapping ("/hello") public String hello () {return "Hallo, Springboot"; }/** * Spring Boot Das Standard -JSON -Auflösungs -Framework ist Jackson * @Return */@RequestMapping ("/getUser") public user getUser () {user u = new user (); U.SetName ("Zhang San"); U.Setage (33); U.SetCreateTime (neues Datum ()); kehre u zurück; }} Paket com.springboot; importieren java.io.serializable; import Java.util.date; Benutzer im öffentlichen Klassen implementiert serialisierbar {private String -Name; privates int Alter; privates Date Createtime; public String getName () {return name; } public void setName (String -Name) {this.name = name; } public int getage () {return ay; } public void setage (int age) {this.age = älter; } public date getCreateTime () {return CreateTime; } public void setCreateTime (Datum CreateTime) {this.createTime = CreateTime; }} Paket 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); }}Start MainApp: Besuchen Sie http: // localhost: 8080/GetUser, das Ergebnis ist wie folgt:
{"Name": "Zhang San", "Alter": 33, "CreateTime": "2018-04-04t03: 03: 08.534+0000"}Es ist zu sehen, dass wir keine Konfiguration vorgenommen haben, aber JSON -Daten zurückgegeben haben. Es ist ersichtlich, dass Spring-Boot eine Standardimplementierung von JSON erstellt und den integrierten Jackson-Konverter verwendet.
Nehmen wir also Fastjson als Beispiel:
Stellen Sie zunächst das Fastjson -Paket ein und fügen Sie die folgenden Abhängigkeiten in die POM hinzu:
<!-https://mvnrepository.com/artifact/com.alibaba/fastjson-> <depeopentcy> <Groupid> com.alibaba </gruppe> <artifactid> fastjson </artifactid> <version> 1.2.47 </Version> </abhängig> </abhängig>
Um den Effekt zu erleichtern, ändern Sie die Benutzerklasse:
Paket com.springboot; importieren java.io.serializable; import Java.util.date; Import com.alibaba.fastjson.annotation.jsonfield; @SuppressWarnings ("Serial") Public Class User implementiert serialisierbare {private String -Name; privates int Alter; @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 ay; } public void setage (int age) {this.age = älter; } public date getCreateTime () {return CreateTime; } public void setCreateTime (Datum CreateTime) {this.createTime = CreateTime; }}1. Die erste Möglichkeit, FastJson Custom JSON-Conversion zu implementieren, implementiert Spring-Boot die WebMVCConventer-Schnittstelle:
Ändern Sie MainApp wie folgt:
Paket com.springboot; Import Java.util.ArrayList; importieren java.util.list; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootApplication; import org.springframework.http.MediType; import org.springframework.http.converter.httpMessageConverter; import org.springframework.web.servlet.config.annotation.webmvcconfiger; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.fastjsonconfig; import com.alibaba.fastjson.support.spring.fastjsonhttpMessageConverter; @SpringbootApplication public class mainApp implementiert webmvcconFigurer {@Override public void configuremessageConverters (Liste <httpMessageConverter <? >> Konverters) {webmvcconfigurer.super.configuremessageConverters (Konverter); // Erstellen Sie eine Fastjson Converter -Instanz FastjsonHttpMessageConverter Converter = New FastjsonHttpMessageConverter (); // Konfigurationsobjekt FASTJSONCONFIG CONFIG = New FastjsonConfig (); List <Mentype> mentypes = new ArrayList <> (); // Chinesische Codierung von mediType mediType = minytype.application_json_utf8; mediateTypes.add (minyType); config.setSerializerFeatures (SerializerFeature.prettyFormat); Converter.SetsupportedMediTypes (mediTypes); Converter.SetFastjsonConfig (config); Converters.Add (Konverter); } public static void main (String [] args) {SpringApplication.run (mainapp.class, args); }}Starten Sie das Programm: Zugriff auf den obigen Pfad: Der Browser sieht die folgenden Ergebnisse fest:
{"Alter": 33, "CreateTime": "2018-04-04 11:14", "Name": "Zhang San"} 2. Injize Fastjson Converter mit @Bean Annotation: MAINAPP wie folgt ändern:
Paket com.springboot; Import Java.util.ArrayList; importieren java.util.list; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootApplication; import org.springframework.boot.autoconfigure.http.httpMessageConverters; import org.springframework.context.annotation.bean; import org.springframework.http.MediType; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.fastjsonconfig; import com.alibaba.fastjson.support.spring.fastjsonhttpMessageConverter; @SpringbootApplication public class mainApp {@bean public httpMessageConverters fastjsonHttpMessageConventers () {fastjsonHttpMessageConverter Converter = new fastjsonHttpMessageConverter (); FastjsonConfig config = new pastjsonConfig (); config.setSerializerFeatures (SerializerFeature.prettyFormat); List <Mentype> mentypes = new ArrayList <> (); in mentypes.add (minyType.application_json_utf8); Converter.SetsupportedMediTypes (mediTypes); RECHTEN SIE NEU HTTPMESSAGECONVERTERTER (Konverter); } public static void main (String [] args) {SpringApplication.run (mainapp.class, args); }}Das Zugangsergebnis ist das gleiche.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.