springBoot 學習記錄(二)-返回json資料的幾種方式
一:新建maven專案
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.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.phpfzh2</groupId>
<artifactId>spring-boot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動類App程式碼:
package com.phpfzh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
實體類程式碼:
package com.phpfzh.mode;
import java.math.BigDecimal;
public class User {
private BigDecimal id;
private String username;
private String sex;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
第一種方式@RestController:
package com.phpfzh.web;
import java.math.BigDecimal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.phpfzh.mode.User;
@RestController
public class TestController {
@RequestMapping("/")
public User test(){
User user = new User();
user.setId(new BigDecimal(12));
user.setSex("12");
user.setUsername("phpfzh");
return user;
}
}
執行專案:
右擊專案 點選 run As ==> Maven clear ====> maven install
右擊專案 點選 run As ===>
java application ===》 在瀏覽器輸入:localhost:8080/
輸出:
{"id":12,"username":"phpfzh","sex":"12"}
第二種方式@ResponseBody:
package com.phpfzh.web;
import java.math.BigDecimal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.phpfzh.mode.User;
@Controller
public class TestController2 {
@ResponseBody
@RequestMapping("/test2")
public User test2(){
User user = new User();
user.setId(new BigDecimal(12));
user.setSex("12");
user.setUsername("phpfzh-test2");
return user;
}
}
執行專案:
右擊專案 點選 run As ==> Maven clear ====> maven install
右擊專案 點選 run As ===> java application ===》 在瀏覽器輸入:localhost:8080/test2
瀏覽器輸出:
{"id":12,"username":"phpfzh-test2","sex":"12"}
第三種方式response:
在pom.xml 新增 阿里巴巴json jar包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.20</version>
</dependency>
Controller程式碼:
package com.phpfzh.web;
import java.io.PrintWriter;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.phpfzh.mode.User;
@Controller
public class TestController3 {
@RequestMapping("/test3")
public String test(HttpServletResponse response){
User user = new User();
user.setId(new BigDecimal(12));
user.setSex("12");
user.setUsername("phpfzh-test3");
String date = JSON.toJSONString(user);
try {
sendJsonData(response, date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void sendJsonData(HttpServletResponse response, String data)
throws Exception
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(data);
out.flush();
out.close();
}
}
執行專案:
右擊專案 點選 run As ==> Maven clear ====> maven install
右擊專案 點選 run As ===> java application ===》 在瀏覽器輸入:localhost:8080/test3
瀏覽器輸出:
{"id":12,"sex":"12","username":"phpfzh-test3"}