18.SPRINGBOOT 做前后端分离
Category
1. 增加个toString() 方便,便于显示
2. 增加个注解:@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" }) ,否则会出错
2. 增加个注解:@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" }) ,否则会出错
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package com.how2java.springboot.pojo;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@Entity@Table(name = "category_")@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" }) public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; @Column(name = "name") private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Category [id=" + id + ", name=" + name + "]"; } } |
步骤 6 :
CategoryController
控制器里提供3个方法,分别用来处理json 提交,json获取单个对象,json获取多个对象
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package com.how2java.springboot.web;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.how2java.springboot.dao.CategoryDAO;import com.how2java.springboot.pojo.Category; @RestControllerpublic class CategoryController { @Autowired CategoryDAO categoryDAO; @GetMapping("/category") public List<Category> listCategory(@RequestParam(value = "start", defaultValue = "0") intstart,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception { start = start<0?0:start; Sort sort = new Sort(Sort.Direction.DESC, "id"); Pageable pageable = new PageRequest(start, size, sort); Page<Category> page =categoryDAO.findAll(pageable); return page.getContent(); } @GetMapping("/category/{id}") public Category getCategory(@PathVariable("id") int id) throws Exception { Category c= categoryDAO.getOne(id); System.out.println(c); return c; } @PutMapping("/category") public void addCategory(@RequestBody Category category) throws Exception { System.out.println("springboot接受到浏览器以JSON格式提交的数据:"+category); }} |
步骤 7 :
submit.html
访问测试地址:
提交成功后,在springboot控制台查看使用json方式提交的数据
注: 不要在eclipse自带的浏览器里面点击,自带的浏览器有bug,有时候不能识别jquery, 会导致点击没有反应。 使用独立的浏览器,比如chrome,firefox点击测试
http://localhost:8080/submit.html
提交成功后,在springboot控制台查看使用json方式提交的数据
注: 不要在eclipse自带的浏览器里面点击,自带的浏览器有bug,有时候不能识别jquery, 会导致点击没有反应。 使用独立的浏览器,比如chrome,firefox点击测试
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>用AJAX以JSON方式提交数据</title><script type="text/javascript" src="js/jquery.min.js"></script></head><body> <form > id:<input type="text" id="id" value="123" /><br/> 名称:<input type="text" id="name" value="category xxx"/><br/> <input type="button" value="提交" id="sender"> </form> <div id="messageDiv"></div> <script> $('#sender').click(function(){ var id=document.getElementById('id').value; var name=document.getElementById('name').value; var category={"name":name,"id":id}; var jsonData = JSON.stringify(category); var page="category"; $.ajax({ type:"put", url: page, data:jsonData, dataType:"json", contentType : "application/json;charset=UTF-8", success: function(result){ } }); alert("提交成功,请在springboot控制台查看服务端接收到的数据"); }); </script></body> </html> |
步骤 8 :
getOne.html
访问测试地址:
注意:要确保id=10的分类对象存在
http://localhost:8080/getOne.html
注意:要确保id=10的分类对象存在
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>用AJAX以JSON方式获取数据</title><script type="text/javascript" src="js/jquery.min.js"></script></head><body> <input type="button" value="通过AJAX获取一个Hero对象---" id="sender"> <div id="messageDiv"></div> <script> $('#sender').click(function(){ var url="category/10"; $.get( url, function(data) { console.log(data); var json=data; var name =json.name; var id = json.id; $("#messageDiv").html("分类id:"+ id + "<br>分类名称:" +name ); }); }); </script></body> </body></html> |
步骤 9 :
getMany.html
访问测试地址:
点击按钮,获取多个json数据
http://localhost:8080/getMany.html
点击按钮,获取多个json数据
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>用AJAX以JSON方式获取数据</title><script type="text/javascript" src="js/jquery.min.js"></script></head><body> <input type="button" value="通过AJAX获取多个分类对象" id="sender"> <div id="messageDiv"></div> <script> $('#sender').click(function(){ var url="category?start=0&size=100"; $.get( url, function(data) { var categorys = data; for(i in categorys){ var old = $("#messageDiv").html(); var category = categorys[i]; $("#messageDiv").html(old + "<br>"+category.id+" ----- "+category.name); } }); }); </script></body> </body></html> |
浙公网安备 33010602011771号