0%

SpringMVC_RESTful

2018年3月31日 上午11:41

对RESTful的理解:

  1. 其实就是要创建一种RESTful风格的URL
  2. 参数全部暴露出来,通过斜杠的方式。_斜杠_就是RESTful的主要表现方式
  3. 原先URL通过.do的方式,URL指向的是一个方法,而RESTful风格的URL指向的是一个资源。
  4. 我理解RESTful就是一种定位到方法的新的方式,这种方式是springMVC本身就支持的。而我们最先使用的detail.do?productId=26这种.do的方式,也是springMVC自带的其中一种而已。

不适合用RESTful的情况:

  1. 方法需要的参数过多的时候,我们就必须在URL中写那么多的参数,看见就不舒服。
  2. RESTful中声明的参数是不可以为空的

SpringMVC_RESTful配置

一些例子:

下面的代码中有两个是bad例子,作为对比,很好理解,我就不带了写了,关键是理解清楚RESTful是干啥的就行了。


资源定位不准确,restfull不予许为空

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.mmall.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.service.IProductService;
import com.mmall.vo.ProductDetailVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Created by geely
*/

@Controller
@RequestMapping("/product/")
public class ProductController {

@Autowired
private IProductService iProductService;



@RequestMapping("detail.do")
@ResponseBody
public ServerResponse<ProductDetailVo> detail(Integer productId){
return iProductService.getProductDetail(productId);
}


@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
@ResponseBody
public ServerResponse<ProductDetailVo> detailRESTful(@PathVariable Integer productId){
return iProductService.getProductDetail(productId);
}

@RequestMapping("list.do")
@ResponseBody
public ServerResponse<PageInfo> list(@RequestParam(value = "keyword",required = false)String keyword,
@RequestParam(value = "categoryId",required = false)Integer categoryId,
@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
@RequestParam(value = "orderBy",defaultValue = "") String orderBy){
return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
}


//http://www.happymmall.com/product/手机/100012/1/10/price_asc
@RequestMapping(value = "/{keyword}/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "keyword")String keyword,
@PathVariable(value = "categoryId")Integer categoryId,
@PathVariable(value = "pageNum") Integer pageNum,
@PathVariable(value = "pageSize") Integer pageSize,
@PathVariable(value = "orderBy") String orderBy){
if(pageNum == null){
pageNum = 1;
}
if(pageSize == null){
pageSize = 10;
}
if(StringUtils.isBlank(orderBy)){
orderBy = "price_asc";
}

return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
}


// http://www.happymmall.com/product/100012/1/10/price_asc
@RequestMapping(value = "/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<PageInfo> listRESTfulBadcase(@PathVariable(value = "categoryId")Integer categoryId,
@PathVariable(value = "pageNum") Integer pageNum,
@PathVariable(value = "pageSize") Integer pageSize,
@PathVariable(value = "orderBy") String orderBy){
if(pageNum == null){
pageNum = 1;
}
if(pageSize == null){
pageSize = 10;
}
if(StringUtils.isBlank(orderBy)){
orderBy = "price_asc";
}

return iProductService.getProductByKeywordCategory("",categoryId,pageNum,pageSize,orderBy);
}


@RequestMapping(value = "/{keyword}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<PageInfo> listRESTfulBadcase(@PathVariable(value = "keyword")String keyword,
@PathVariable(value = "pageNum") Integer pageNum,
@PathVariable(value = "pageSize") Integer pageSize,
@PathVariable(value = "orderBy") String orderBy){
if(pageNum == null){
pageNum = 1;
}
if(pageSize == null){
pageSize = 10;
}
if(StringUtils.isBlank(orderBy)){
orderBy = "price_asc";
}

return iProductService.getProductByKeywordCategory(keyword,null,pageNum,pageSize,orderBy);
}


//http://www.happymmall.com/product/keyword/手机/1/10/price_asc
@RequestMapping(value = "/keyword/{keyword}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "keyword")String keyword,
@PathVariable(value = "pageNum") Integer pageNum,
@PathVariable(value = "pageSize") Integer pageSize,
@PathVariable(value = "orderBy") String orderBy){
if(pageNum == null){
pageNum = 1;
}
if(pageSize == null){
pageSize = 10;
}
if(StringUtils.isBlank(orderBy)){
orderBy = "price_asc";
}

return iProductService.getProductByKeywordCategory(keyword,null,pageNum,pageSize,orderBy);
}


//http://www.happymmall.com/product/category/100012/1/10/price_asc
@RequestMapping(value = "/category/{categoryId}/{pageNum}/{pageSize}/{orderBy}",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<PageInfo> listRESTful(@PathVariable(value = "categoryId")Integer categoryId,
@PathVariable(value = "pageNum") Integer pageNum,
@PathVariable(value = "pageSize") Integer pageSize,
@PathVariable(value = "orderBy") String orderBy){
if(pageNum == null){
pageNum = 1;
}
if(pageSize == null){
pageSize = 10;
}
if(StringUtils.isBlank(orderBy)){
orderBy = "price_asc";
}

return iProductService.getProductByKeywordCategory("",categoryId,pageNum,pageSize,orderBy);
}
}