SpringMVCRestful

Restful是一种开发理念,是对http请求的解释,希望以非常简洁的url地址来请求
  1. 对URL进行规范,写Restful风格的url:
  2. 对http方法规范:
    不管是添加、更新、删除、查询、使用的url都是一致的,访问时根据请求方式进行区分:delete(删除)、post(添加)、get(查询),而在后台对请求方式进行判断,执行相应的方法;
  3. 对http的contentType进行规范:
    请求时指定contentType,要什么格式就设置为什么格式,要JSON就设置为JSON
》》在实际的开发中,由于controller方法中要进行判断,处理过于繁琐,所以url参数传递和JSON处理应用多一些;

******************************************************************************************

1. 依赖包:

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.9</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.9</version>
  </dependency>

2. 代码实现:

@ResponseBody   //此注解的意义在于对象直接以json返回
@RequestMapping("get/{id}")//将参数封装到访问地址,参数不能为空
public Object get(@PathVariable("id") long id,String name) {
    return "hello word Restful" + id + name ;
}

3. 浏览器访问:

http://localhost:8080/pages/message/get/1
原文地址:https://www.cnblogs.com/luliang888/p/11074347.html