RESTful编码风格

什么是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")
注意两个区别
1.@PathVariable是获取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;
    }

}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值