First, we create a maven project, as shown in the following figure:
Step 2: configure pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- Specify jdk We use jdk 1.7 ,The default is 1..6 --> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Add to fastjson Dependency package. --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <!-- spring boot devtools Dependency package. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies>
Step 3: create the package structure and class as shown in the following figure
Step 4: write the above three classes
User:
Provide get and set methods.
FastJsonController:
App:
After the above steps are completed, execute the main method of App class and wait for the project to start. Then access in the browser
http://localhost:8080/getJson, the returned result is as follows:
When Chinese code is found, modify the controller class, as shown in the following figure:
Then visit again, the results are as follows:
Solve the confusion.