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);
} }

最新文章

  1. 重温JSP学习笔记--JSTL标签库
  2. 【面试】http协议知识
  3. ios枚举规范
  4. mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存
  5. BizTalk开发系列(十六) XML命名空间
  6. 为什么drop table的时候要在checking permissions花很长时间?
  7. SQL语句技巧(上个样式太差了)
  8. 更改pip源
  9. Bootstrap入门(二十)组件14:警告框
  10. 小型互联网公司的IT系统建设思路
  11. 北航MOOC客户端
  12. Python中self和__init__的含义与使用
  13. PLSA主题模型
  14. saliency map [转]
  15. mybatis 插件原理
  16. php array_rand()函数从数组中随机选择一个或多个元素
  17. xshell分隔符
  18. Egret3D初步学习笔记四 (地形使用)
  19. bzoj 3522 tree-dp 暴力
  20. 【PM】关于系统数据库和服务现场升级的一些看法

热门文章

  1. Linux Bash Shell j简单入门
  2. [Java复习] 分布式PRC - Dubbo
  3. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_16-认证接口开发-Api接口定义
  4. MySQL设置可以远程连接
  5. 使用httpwebrequest Post数据到网站
  6. tomcat 是如何做到不同webapp 类隔离的
  7. LNMP V1.4正式版本安装及新增Let&#39;s Encrypt一键安装和其他功能
  8. [转帖]MMU内存管理单元
  9. [转帖]ASP.NET Core 中间件(Middleware)详解
  10. AtCoder整理(持续更新中……)