RESTful编码风格
程序员文章站
2024-03-25 16:59:10
...
什么是restful?
RESTful建议请求需要区分GET、POST、PUT等;返回的数据建议是JSON;网络协议使用https;请求url包含版本号等等。
RESTful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
京东就使用的这种编程风格,URL 占位符。
从URL上获取参数,请求参数在URL上:
使用RESTful风格开发的接口,根据id查询商品,接口地址是:http://127.0.0.1/item/1
@RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
从url上获取商品id,步骤如下:
1.使用注解@RequestMapping("item/{id}")声明请求的url
{xxx}叫做占位符,请求的URL可以是“item /1”或“item/2”
2.使用(@PathVariable() Integer id)获取url上的数据。
如果@RequestMapping中表示为"item/{id}",id和形参名称一致,@PathVariable不用指定名称。如果不一致,例如"item/{ItemId}"则需要指定名称@PathVariable("itemId")。
注意两个区别
[email protected]是获取url上数据的。@RequestParam获取请求参数的(包括post表单提交)
2.如果加上@ResponseBody注解,就不会走视图解析器,不会返回页面,目前返回的json数据。如果不加,就走视图解析器,返回页面
代码如下:
RESTful建议请求需要区分GET、POST、PUT等;返回的数据建议是JSON;网络协议使用https;请求url包含版本号等等。
RESTful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
京东就使用的这种编程风格,URL 占位符。
从URL上获取参数,请求参数在URL上:
使用RESTful风格开发的接口,根据id查询商品,接口地址是:http://127.0.0.1/item/1
@RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
从url上获取商品id,步骤如下:
1.使用注解@RequestMapping("item/{id}")声明请求的url
{xxx}叫做占位符,请求的URL可以是“item /1”或“item/2”
2.使用(@PathVariable() Integer id)获取url上的数据。
如果@RequestMapping中表示为"item/{id}",id和形参名称一致,@PathVariable不用指定名称。如果不一致,例如"item/{ItemId}"则需要指定名称@PathVariable("itemId")。
注意两个区别
[email protected]是获取url上数据的。@RequestParam获取请求参数的(包括post表单提交)
2.如果加上@ResponseBody注解,就不会走视图解析器,不会返回页面,目前返回的json数据。如果不加,就走视图解析器,返回页面
代码如下:
package com.awb.controller;
import com.awb.pojo.Items;
import com.awb.pojo.Student;
import com.awb.service.ProductServiceImpl;
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.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping(value = "/product")
public class RestfulController {
//在Controller中编写接口,这里供设计了三个接口,分别返回字符串,JSON对象,JSON数组.
@Autowired
private ProductServiceImpl mProductServiceImpl;
//http://localhost:8080/product/getAll/1
//获取url上传递的参数值
//解决返回值乱码问题:produces = "application/json;charset=utf-8"
@RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
@ResponseBody
public String getUserInfo(@PathVariable("id") String pid){
Items items = mProductServiceImpl.queryProductById(Integer.parseInt(pid));
return items.toString();
}
/**
* 返回对象,自动转化为JSON格式
* @param teamname
* @param request
* @return
*/
@RequestMapping(value = "v2/new/{teamname}",method = RequestMethod.GET)
@ResponseBody
public Student getUserInfoFir(@PathVariable String teamname, HttpServletRequest request){
String name = request.getParameter("name");
String age = request.getParameter("age");
Student student = new Student(name, age);
return student;
}
/**
* 直接返回List,自动转化为JSON格式
* @param teamname
* @param request
* @return
*/
@RequestMapping(value = "v3/new/{teamname}", method = RequestMethod.GET)
@ResponseBody
public List<Student> getUserInfoSec(@PathVariable String teamname, HttpServletRequest request) {
//可以使用teamname获取url路径分隔
//获取请求的参数
String name = request.getParameter("name");
String age = request.getParameter("age");
Student student = new Student(name, age);
List<Student> list = new ArrayList<>();
list.add(student);
return list;
}
}