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);
} }
05-23 15:21