美文网首页我爱编程实习日记
Spring学习笔记之RESTful

Spring学习笔记之RESTful

作者: DerrickWang | 来源:发表于2018-05-25 16:08 被阅读0次
  • RESTful
    representaitonal state transfer(表象性状态转变/表述性状态转变)
    REST是web服务的一种架构风格,使用HTTP URI XML JSON HTML等广泛流行的标准和协议
    REST是一种轻量级、跨平台、跨语言的架构设计
    REST是一种设计风格,是一种思想,不是一种标准

RESTful的web服务是一种ROA(面向资源的架构)

REST架构的主要原则

网络上的所有事物都被抽象为资源
每个资源都有一个唯一的资源标识符
同一个资源具有多种表现形式(xml json等)
对资源的各种操作都不会改变资源标识符
所有操作都是无状态的
符合REST原则的架构方式即可称为RESTful

  • 例子SpringMVC实现restful服务
  
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 cn.itcast.mybatis.pojo.User;  
import cn.itcast.mybatis.service.NewUserService;  
  
@RequestMapping("restful/user")  
@Controller  
public class RestUserController {  
  
    @Autowired  
    private NewUserService newUserService;  
  
    /** 
     * 根据用户id查询用户数据 
     *  
     * @param id 
     * @return 
     */  
    @RequestMapping(value = "{id}", method = RequestMethod.GET)  
    @ResponseBody  
    public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {  
        try {  
            User user = this.newUserService.queryUserById(id);  
            if (null == user) {  
                // 资源不存在,响应404  
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);  
            }  
            // 200  
            // return ResponseEntity.status(HttpStatus.OK).body(user);  
            return ResponseEntity.ok(user);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        // 500  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
    }  
  
    /** 
     * 新增用户 
     *  
     * @param user 
     * @return 
     */  
    @RequestMapping(method = RequestMethod.POST)  
    public ResponseEntity<Void> saveUser(User user) {  
        try {  
            this.newUserService.saveUser(user);  
            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);  
    }  
  
    /** 
     * 更新用户资源 
     *  
     * @param user 
     * @return 
     */  
    @RequestMapping(method = RequestMethod.PUT)  
    public ResponseEntity<Void> updateUser(User user) {  
        try {  
            this.newUserService.updateUser(user);  
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        // 500  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
    }  
  
    /** 
     * 删除用户资源 
     *  
     * @param user 
     * @return 
     */  
    @RequestMapping(method = RequestMethod.DELETE)  
    public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {  
        try {  
            if (id.intValue() == 0) {  
                // 请求参数有误  
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();  
            }  
            this.newUserService.deleteUserById(id);  
            // 204  
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        // 500  
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
    }  
}  

相关文章

网友评论

    本文标题:Spring学习笔记之RESTful

    本文链接:https://www.haomeiwen.com/subject/mzhujftx.html