package com.aaaaaa.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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; import com.aaaaaa.manager.pojo.Item;
import com.aaaaaa.manager.service.ItemService; @Controller
@RequestMapping("item/interface")
public class ItemInterfaceController {
// http://127.0.0.1/query/1?rows=2
// @RequestParam:获取请求参数的数据(包括表单提交的数据),就是获取rows=2 的2
// @PathVariable:获取请求url路径上的数据,就是1 @Autowired
private ItemService itemService; // 根据id查询 GET
// http://manager.aaaaaa.com/rest/item/interface/{id}
/**
* 根据id查询
*
* @param id
* @return
*/
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Item> queryItemById(@PathVariable Long id) {
try {
Item item = this.itemService.queryById(id);
// 查询成功,返回200
// return ResponseEntity.status(HttpStatus.OK).body(item);
// return ResponseEntity.ok().body(item);
return ResponseEntity.ok(item);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 如果服务器出错,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
} // 新增 POST
// http://manager.aaaaaa.com/rest/item/interface
/**
* 新增
*
* @param item
* @return
*/
@RequestMapping(method = RequestMethod.POST)
// @ResponseBody:不加这个注解就会走视图解析器,返回页面,加这个注解就走转换器,返回数据
// 加上@ResponseBody注解和返回ResponseEntity效果是一样的,都会走转换器,返回数据,所以使用任意一个即可,两个都用也没问题
public ResponseEntity<Void> saveItem(Item item) {
try {
this.itemService.save(item);
// 新增成功,返回201
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 如果服务器出错,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
} // 更新 PUT
// http://manager.aaaaaa.com/rest/item/interface
/**
* 更新
*
* @param item
* @return
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<Void> updateItem(Item item) {
try {
this.itemService.updateByIdSelective(item);
// 修改成功,返回204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 如果服务器出错,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
} // 根据id删除 DELETE
// http://manager.aaaaaa.com/rest/item/interface/{id}
/**
* 根据id删除
*
* @param id
* @return
*/
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteItemById(@PathVariable Long id) {
try {
this.itemService.deleteById(id);
// 删除成功,返回204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 如果服务器出错,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
} }

RESTful接口开发的更多相关文章

  1. python测试开发django-rest-framework-59.restful接口开发

    前言 REST 不是什么具体的软件或者代码,而是一种思想.现在流行前后端分离开发项目,一般用 json 来交换数据. 相信写过模板的同学都知道,只要哪怕页面中的数据有一丝丝变动,那整个页面都需要重新渲 ...

  2. RESTful接口开发规范

    最近在研究restful,公司开发要使用,所以自己就去网上找了好些资料,并整理了一套公司开发的接口规范.当然,我也只是刚刚入坑.还不是很全面.但是这就是一个过程.一点点,总会好起来的.以下是就是RES ...

  3. Restful 接口开发 完整版

    准备: springboot ssm框架 项目结构:注意 Swagger2Config要和springboot的启动类放在同一级 //pom所需的依赖 <dependency> <g ...

  4. python 全栈开发,Day100(restful 接口,DRF组件,DRF跨域(cors组件))

    昨日内容回顾 1. 为什么要做前后端分离? - 前后端交给不同的人来编写,职责划分明确.方便快速开发 - 针对pc,手机,ipad,微信,支付宝... 使用同一个接口 2. 简述http协议? - 基 ...

  5. SpringMVC开发RESTful接口

    概念: 什么是REST? REST是Representational State Transfer的缩写.翻译为"表现层状态转化",restful是一种接口设计风格,它不是一个协议 ...

  6. 接口开发-restful

    数据库表设计 1 --员工表 2 create table Employee 3 ( 4 id NUMBER primary key, 5 employeeID NUMBER not null, 6 ...

  7. API接口开发 配置、实现、测试

    Yii2 基于RESTful架构的 advanced版API接口开发 配置.实现.测试 环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到 ...

  8. RESTful接口设计原则和优点

    RESTful架构优点: 前后端分离,减少流量 安全问题集中在接口上,由于接受json格式,防止了注入型等安全问题 前端无关化,后端只负责数据处理,前端表现方式可以是任何前端语言(android,io ...

  9. Swagger文档化restful接口

    1.注解 @Api:用在类上,说明该类的作用. @ApiOperation:注解来给API增加方法说明. @ApiImplicitParams : 用在方法上包含一组参数说明. @ApiImplici ...

随机推荐

  1. 阶段5 3.微服务项目【学成在线】_day18 用户授权_04-方法授权-方法授权实现

    2.3 方法授权实现 2.3.1资源服务添加授权控制 1.要想在资源服务使用方法授权,首先在资源服务配置授权控制 1)添加spring-cloud-starter-oauth2依赖. 2)拷贝授权配置 ...

  2. 算法习题---4-3黑白棋(UVa220)

    一:题目 系统提示当前旗手W/B(白/黑)下子,例如W下子,那么W下的位置必须是夹住黑色棋子的位置才可以. 夹住方式:横向.竖向.斜向 注意落子后将夹住的黑棋吞噬变为白棋 (一)题目详解 .棋盘以数组 ...

  3. HashSet的实现原理,简单易懂

    HashSet的实现原理,简单易懂   答: HashSet实际上是一个HashMap实例,都是一个存放链表的数组.它不保证存储元素的迭代顺序:此类允许使用null元素.HashSet中不允许有重复元 ...

  4. web手工项目02-注册功能输入分析,处理,输出方法-测试用例及缺陷编写-首页轮播图和购物车

    web手工项目第二天笔记 昨日回顾 搭建测试环境(WAMP,phpStudy,tpshop项目文件) 熟悉项目(四个步骤,三个来源) 项目测试流程(需求评审,测试计划与方案,测试用例设计与评审,测试执 ...

  5. RocketMQ共包含9个模块

    rocketmq-common:通用的枚举.基类方法.或者数据结构,包名有admin.consumer.filter.hook.message rocketmq-remoting:使用netty的客户 ...

  6. 【Leetcode_easy】784. Letter Case Permutation

    problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...

  7. 【Leetcode_easy】1089. Duplicate Zeros

    problem 1089. Duplicate Zeros 题意: solution: 其中关于虚拟新数组的下标的计算还是有点迷糊... class Solution { public: void d ...

  8. Windows 10在2018四月更新中默认安装了OpenSSH客户端

    客户端安装路径 C:\Windows\System32\OpenSSH 版本:

  9. Burpsuite—渗透测试神器

    Google浏览器插件---SwitchyOmega Firefox浏览器插件---SwitchyOmega hosts代理工具---SwitchHosts[右击使用管理员权限打开] 双击burp-l ...

  10. win7 vs2010 opengl配置教程

    一.安装GL库文件 1. opengl和glu的安装(不用安装) win7安装完成之后已经默认安装了opengl32.dll和glu32.dll,并且其对应的lib文件也已经安装