笔记67 Spring Boot快速入门(七)

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.

三、

猜你喜欢

转载自www.cnblogs.com/lyj-gyq/p/9314425.html