码迷,mamicode.com
首页 > 其他好文 > 详细

RESTful接口开发

时间:2017-10-06 23:10:36      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:param   ror   ppi   color   tps   nbsp   work   视图   mapping   

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接口开发

标签:param   ror   ppi   color   tps   nbsp   work   视图   mapping   

原文地址:http://www.cnblogs.com/javaxiaoxin/p/7633090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!