SpringBoot入門 - web工程 - 獲取後臺返回字串 物件 及json資料
阿新 • • 發佈:2019-01-04
下面講解springboot-web工程 如何跳轉到一個頁面 和 獲取一個後臺返回的字串,物件,以及json資料...
1.建立一個maven web工程
2.匯入依賴
<!-- web支援: 1、web mvc; 2、restful; 3、jackjson支援; 4、aop ... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet依賴 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat 的支援. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
溫馨小提示:這裡我是多模組專案
我是在外面引入的 SpringBoot所需依賴
<!-- dependencyManagement-解決單繼承問題 --> <dependencyManagement> <dependencies> <!--springboot版本管理--><!-- SpringBoot所需依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
3. 配置application.properties對jsp支援-前後綴
# 頁面預設字首目錄
spring.mvc.view.prefix=/WEB-INF/views/
# 響應頁面預設字尾
spring.mvc.view.suffix=.jsp
# 自定義屬性,可以在Controller中讀取
application.hello=Hello Angel From application
4.編寫jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>user頁面</title> </head> <body> ${msg} </body> </html>
5.編寫controller類
@Controller
public class WebController {
@RequestMapping("/str")
@ResponseBody
public String json1(){
return "字串";
}
@RequestMapping("/obj")
@ResponseBody
public Person json2(){
return new Person(1L,"物件-日期",new Date());
}
@RequestMapping("/array")
@ResponseBody
public List<Person> json3(){
return Arrays.asList(new Person(1L,"陣列-資料1",new Date()),new Person(2L,"陣列-資料2",new Date()));
}
@RequestMapping("/index")
public String jsp(Model model){
model.addAttribute("msg", "後臺傳的資料...");
return "user/index";
}
}
溫馨小提示:官方推薦使用 @RestController[email protected][email protected] 如下使用
@RestController //@[email protected][email protected] 官方推薦就使用 public class WebController2 { @RequestMapping("/str2") public String json1(){ return "字串"; } @RequestMapping("/obj2") public Person json2(){ return new Person(1L,"物件-日期",new Date()); } @RequestMapping("/array2") public List<Person> json3(){ return Arrays.asList(new Person(1L,"陣列-資料1",new Date()),new Person(2L,"陣列-資料2",new Date())); } }
這裡我用到的domain實體類
public class Person {
private Long id;
private String name;
private Date birthDay;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public Person(Long id, String name, Date birthDay) {
this.id = id;
this.name = name;
this.birthDay = birthDay;
}
}
6.編寫啟動類
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class);
}
}
7.啟動App並測試
注意:這裡通過路徑返回jsp頁面好像有問題,看了一下資料,發現好像是springboot的版本問題,好像1.4版本以下可以用,官方推薦我們使用模板引擎技術,不推薦使用jsp
使用Freemaker模板跳轉頁面可參考:https://blog.csdn.net/qq_38225558/article/details/85761281
最後附上專案整體結構: