筆記67 Spring Boot快速入門(七)
阿新 • • 發佈:2018-07-15
判斷瀏覽器 業務 src tar rate sort col font list
SpringBoot+Restful+JSON
一、Restful風格
在Web開發的過程中,method常用的值是get和post。可事實上,method值還可以是put和delete等等其他值。
既然method值如此豐富,那麽就可以考慮使用同一個url,但是約定不同的method來實施不同的業務,這就是Restful的基本考慮。
CRUD是最常見的操作,在使用Restful 風格之前,通常的增加做法是這樣的:/addCategory?name=xxx
可是使用了Restuful風格之後,增加就變成了:/category
CRUD如下表所示,URL就都使用一樣的 "/category",區別只是在於method不同,服務器根據method的不同來判斷瀏覽器期望做的業務行為。
二、分別是以json方式:提交,獲取單個和獲取多個
提交:http://localhost:8080/submit.html
獲取單個:http://localhost:8080/getOne.html
獲取多個:http://localhost:8080/getMany.html
基於Restful 風格的springboot進行修改。
1.修改Category.java
①增加個toString() 方便,便於顯示
②增加個註解:@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" }) ,否則會出錯
1 package com.example.springbootjpademo.pojo;2 3 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 5 import javax.persistence.*; 6 7 @Entity 8 @Table(name = "category") 9 @JsonIgnoreProperties({ "handler","hibernateLazyInitializer" }) 10 public class Category { 11 @Id 12 @GeneratedValue(strategy = GenerationType.IDENTITY)13 @Column(name = "id") 14 private int id; 15 16 @Column(name = "name") 17 private String name; 18 public int getId() { 19 return id; 20 } 21 public void setId(int id) { 22 this.id = id; 23 } 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 public String toString() { 32 return "Category [id=" + id + ", name=" + name + "]"; 33 } 34 }
2.CategoryController.java
控制器裏提供3個方法,分別用來處理json 提交,json獲取單個對象,json獲取多個對象。
1 package com.example.springbootjpademo.controller; 2 3 import com.example.springbootjpademo.dao.CategoryDAO; 4 import com.example.springbootjpademo.pojo.Category; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.data.domain.Page; 7 import org.springframework.data.domain.PageRequest; 8 import org.springframework.data.domain.Pageable; 9 import org.springframework.data.domain.Sort; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.ui.Model; 12 import org.springframework.web.bind.annotation.*; 13 14 import java.util.List; 15 @Controller 16 //@RestController 17 public class CategoryController { 18 @Autowired 19 CategoryDAO categoryDAO; 20 21 @GetMapping("/category") 22 public List<Category> listCategory(@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception { 23 start = start<0?0:start; 24 Sort sort = new Sort(Sort.Direction.DESC, "id"); 25 Pageable pageable = new PageRequest(start, size, sort); 26 Page<Category> page =categoryDAO.findAll(pageable); 27 return page.getContent(); 28 } 29 30 @GetMapping("/category/{id}") 31 public Category getCategory(@PathVariable("id") int id) throws Exception { 32 Category c= categoryDAO.getOne(id); 33 System.out.println(c); 34 return c; 35 } 36 @PutMapping("/category") 37 public void addCategories(@RequestBody Category category) throws Exception { 38 Category category1=new Category(); 39 category1.setName(category.getName()); 40 categoryDAO.save(category1); 41 System.out.println("springboot接受到瀏覽器以JSON格式提交的數據:"+category); 42 } 43 }
3.
三、
筆記67 Spring Boot快速入門(七)