1、restful

    restful不是一种新技术,而是一种编程风格,一种约定RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。

GET   一般是用来做查询的,查询单个对象
POST     一般用来做修改的
DELETE   一般用来做删除
PUT      一般用来做新增
PATCH    一般用来操作批量数据的

例如:

@Controller
@RequestMapping("/department")//定位资源
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //查询所有接口url,前台传递/department/list
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    public List<Department> findAll() {
        return departmentService.findAll();
    }
    //新增保存
    @RequestMapping(value = "/save" ,method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"保存失败");
        }
    }
    //删除接口url,前台传递/department/delete/1
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    //修改
    @RequestMapping(value = "/update" ,method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Department department){
        try {
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"保存失败");
        }
    }
    //查询单个
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Department findByID(@PathVariable("id") Long id){
        return departmentService.findOneById(id);
    }
}

2、swagger

    Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件,随着现在许多公司实现了前后端分离,swagger越来越受欢迎了。

2.1、jar包

<properties>
        <!--swagger对应的版本-->
        <springfox.version>2.4.0</springfox.version>
    </properties>
<dependencies>
	 <dependency>
	            <groupId>io.springfox</groupId>
	            <artifactId>springfox-swagger2</artifactId>
	            <version>${springfox.version}</version>
	        </dependency>
	        <dependency>
	            <groupId>io.springfox</groupId>
	            <artifactId>springfox-swagger-ui</artifactId>
	            <version>${springfox.version}</version>
	        </dependency>
</dependencies>

2.2、SwaggerConfig

    在web层 新创建包com.sevens.crm.web.config和类:SwaggerConfig
SwaggerConfig

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages="com.sevens.crm.web.controller")
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sevens.crm.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiInfo apiInfo(){
         @SuppressWarnings("deprecation")
        ApiInfo info=new ApiInfo(
                 "CRM所需要的全部接口",
                 "测试接口",
                 "1.0",
                 "www.sevens.com",
                 "seven",
                 "7",
                 "7");
         return info;
    }
}

restcontroller post 单个参数 restful post get_spring


restcontroller post 单个参数 restful post get_数据_02


不要忘记扫描包

<!--扫描swagger-->
    <context:component-scan base-package="com.sevens.crm.web.config"/>

2.3、运行

http://localhost/swagger-ui.html

2.4、效果

restcontroller post 单个参数 restful post get_restful_03

3、postman

    Postman 提供功能强大的 Web API 和 HTTP 请求的调试,它能够发送任何类型的HTTP 请求 (GET, POST, PUT, DELETE…),并且能附带任何数量的参数和 Headers。不仅如此,它还提供测试数据和环境配置数据的导入导出,付费的 Post Cloud 用户还能够创建自己的 Team Library 用来团队协作式的测试,并能够将自己的测试收藏夹和用例数据分享给团队。
官方下载:https://www.getpostman.com/

3.1、安装

下载之后打开需要注册

restcontroller post 单个参数 restful post get_restful_04

3.2、查询所有(无参)

选择对应的url,和参数。也可以保存

restcontroller post 单个参数 restful post get_restful_05

3.3、根据id查询一个(传参id)

restcontroller post 单个参数 restful post get_数据_06

3.4、新增添加(json格式)

restcontroller post 单个参数 restful post get_restful_07