SpringMVC实现RESTful服务

原贴地址:http://blog.csdn.net/zbw18297786698/article/details/53946201


1、SpringMVC实现RESTful服务

  1. SpringMVC原生态的支持了REST风格的架构设计。
  2. 所涉及到的注解和类
  • @RequestMapping
  • @PathVariable
  • @ResponseBody
  • ResponseEntity
  • HttpStatus
  • ……

2、查询资源

     2.1 查询资源的代码实现

[java] view plain copy
  1. @RequestMapping(value = "{id}", method = RequestMethod.GET)  
  2.     public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {  
  3.         try {  
  4.             User user = this.userService.queryUserById(id);  
  5.             if (null == user) {  
  6.                 // 资源不存在 404  
  7.                 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);  
  8.             }  
  9.             // 资源存在 200  
  10.             return ResponseEntity.status(HttpStatus.OK).body(user);  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         // 出现异常,服务器内部错误  
  15.         // 500  
  16.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
  17.     }  

     2.2 查询资源的测试

           要查询的资源存在

           

           要查询的资源不存在

         

     2.3 使用忽略@JsonIgnore,在返回User时,忽略密码属性的信息

           

3、新增资源

3.1新增资源的代码实现

[java] view plain copy
  1. /** 
  2.      * 新增资源 
  3.      *  
  4.      * @param user 
  5.      * @return 
  6.      */  
  7.     @RequestMapping(method = RequestMethod.POST)  
  8.     public ResponseEntity<Void> saveUser(User user) {  
  9.         try {  
  10.             this.userService.saveUser(user);  
  11.             return ResponseEntity.status(HttpStatus.CREATED).build();  
  12.         } catch (Exception e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.         // 出现异常,服务器内部错误  
  16.         // 500  
  17.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  18.                 null);  
  19.     }  

3.2新增资源的测试

                 

4、更新资源

       4.0 默认情况下,PUT请求是无法提交表单数据的,需要在web.xml中添加过滤器解决

[html] view plain copy
  1.    <!-- 解决PUT请求无法提交表单数据的问题 -->  
  2. <filter>  
  3.     <filter-name>HttpMethodFilter</filter-name>  
  4.     <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>  
  5. </filter>  
  6. <filter-mapping>  
  7.     <filter-name>HttpMethodFilter</filter-name>  
  8.     <url-pattern>/*</url-pattern>  
  9. </filter-mapping>  

4.1 更新资源的代码实现

[java] view plain copy
  1. /** 
  2.      * 修改资源 
  3.      *  
  4.      * @param user 
  5.      * @return 
  6.      */  
  7.     @RequestMapping(method = RequestMethod.PUT)  
  8.     public ResponseEntity<Void> updateUser(User user) {  
  9.         try {  
  10.             this.userService.updateUser(user);  
  11.             // 204  
  12.             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.         // 出现异常,服务器内部错误  
  17.         // 500  
  18.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  19.                 null);  
  20.     }  

4.2 更新资源的测试

   

5、删除资源

       5.0 默认情况下,Delete请求是无法提交表单数据的,需要在web.xml中添加过滤器解决

[html] view plain copy
  1. <!-- 将POST请求转化为DELETE或者是PUT 要用_method指定真正的请求参数 -->  
  2.     <filter>  
  3.         <filter-name>HiddenHttpMethodFilter</filter-name>  
  4.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  5.     </filter>  
  6.     <filter-mapping>  
  7.         <filter-name>HiddenHttpMethodFilter</filter-name>  
  8.         <url-pattern>/*</url-pattern>  
  9.     </filter-mapping>  

5.1删除资源的代码实现

[java] view plain copy
  1. /** 
  2.      * 删除资源 
  3.      *  
  4.      * @param user 
  5.      * @return 
  6.      */  
  7.     @RequestMapping(method = RequestMethod.DELETE)  
  8.     public ResponseEntity<Void> deleteUser(  
  9.             @RequestParam(value = "id", defaultValue = "0") Long id) {  
  10.         try {  
  11.             this.userService.deleteById(id);  
  12.             // 204  
  13.             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
  14.         } catch (Exception e) {  
  15.             e.printStackTrace();  
  16.         }  
  17.         // 出现异常,服务器内部错误  
  18.         // 500  
  19.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  20.                 null);  
  21.     }  

5.2 删除资源的测试

       

6、完整的实现代码

[java] view plain copy
  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.http.HttpStatus;  
  3. import org.springframework.http.ResponseEntity;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.PathVariable;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestMethod;  
  8. import org.springframework.web.bind.annotation.RequestParam;  
  9. import cn.itcast.user.pojo.User;  
  10. import cn.itcast.user.service.UserService;  
  11.   
  12. @RequestMapping("new/user")  
  13. @Controller  
  14. public class RestUserController {  
  15.   
  16.     @Autowired  
  17.     private UserService userService;  
  18.   
  19.     @RequestMapping(value = "{id}", method = RequestMethod.GET)  
  20.     public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {  
  21.         try {  
  22.             User user = this.userService.queryUserById(id);  
  23.             if (null == user) {  
  24.                 // 资源不存在 404  
  25.                 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);  
  26.             }  
  27.             // 资源存在 200  
  28.             return ResponseEntity.status(HttpStatus.OK).body(user);  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.         // 出现异常,服务器内部错误  
  33.         // 500  
  34.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  35.                 null);  
  36.     }  
  37.   
  38.     /** 
  39.      * 新增资源 
  40.      *  
  41.      * @param user 
  42.      * @return 
  43.      */  
  44.     @RequestMapping(method = RequestMethod.POST)  
  45.     public ResponseEntity<Void> saveUser(User user) {  
  46.         try {  
  47.             this.userService.saveUser(user);  
  48.             // 201  
  49.             return ResponseEntity.status(HttpStatus.CREATED).build();  
  50.         } catch (Exception e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.         // 出现异常,服务器内部错误  
  54.         // 500  
  55.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  56.                 null);  
  57.     }  
  58.   
  59.     /** 
  60.      * 修改资源 
  61.      *  
  62.      * @param user 
  63.      * @return 
  64.      */  
  65.     @RequestMapping(method = RequestMethod.PUT)  
  66.     public ResponseEntity<Void> updateUser(User user) {  
  67.         try {  
  68.             this.userService.updateUser(user);  
  69.             // 204  
  70.             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
  71.         } catch (Exception e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.         // 出现异常,服务器内部错误  
  75.         // 500  
  76.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  77.                 null);  
  78.     }  
  79.   
  80.     /** 
  81.      * 删除资源 
  82.      *  
  83.      * @param user 
  84.      * @return 
  85.      */  
  86.     @RequestMapping(method = RequestMethod.DELETE)  
  87.     public ResponseEntity<Void> deleteUser(  
  88.             @RequestParam(value = "id", defaultValue = "0") Long id) {  
  89.         try {  
  90.             this.userService.deleteById(id);  
  91.             // 204  
  92.             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
  93.         } catch (Exception e) {  
  94.             e.printStackTrace();  
  95.         }  
  96.         // 出现异常,服务器内部错误  
  97.         // 500  
  98.         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(  
  99.                 null);  
  100.     }  
  101. }  

7、源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值