原贴地址:http://blog.csdn.net/zbw18297786698/article/details/53946201
1、SpringMVC实现RESTful服务
- SpringMVC原生态的支持了REST风格的架构设计。
- 所涉及到的注解和类
- @RequestMapping
- @PathVariable
- @ResponseBody
- ResponseEntity
- HttpStatus
- ……
2、查询资源
2.1 查询资源的代码实现
- @RequestMapping(value = "{id}", method = RequestMethod.GET)
- public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
- try {
- User user = this.userService.queryUserById(id);
- if (null == user) {
- // 资源不存在 404
- return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
- }
- // 资源存在 200
- return ResponseEntity.status(HttpStatus.OK).body(user);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 出现异常,服务器内部错误
- // 500
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
- }
2.2 查询资源的测试
要查询的资源存在
要查询的资源不存在
2.3 使用忽略@JsonIgnore,在返回User时,忽略密码属性的信息
3、新增资源
3.1新增资源的代码实现
- /**
- * 新增资源
- *
- * @param user
- * @return
- */
- @RequestMapping(method = RequestMethod.POST)
- public ResponseEntity<Void> saveUser(User user) {
- try {
- this.userService.saveUser(user);
- return ResponseEntity.status(HttpStatus.CREATED).build();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 出现异常,服务器内部错误
- // 500
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
- null);
- }
3.2新增资源的测试
4、更新资源
4.0 默认情况下,PUT请求是无法提交表单数据的,需要在web.xml中添加过滤器解决
- <!-- 解决PUT请求无法提交表单数据的问题 -->
- <filter>
- <filter-name>HttpMethodFilter</filter-name>
- <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>HttpMethodFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
4.1 更新资源的代码实现
- /**
- * 修改资源
- *
- * @param user
- * @return
- */
- @RequestMapping(method = RequestMethod.PUT)
- public ResponseEntity<Void> updateUser(User user) {
- try {
- this.userService.updateUser(user);
- // 204
- return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 出现异常,服务器内部错误
- // 500
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
- null);
- }
4.2 更新资源的测试
5、删除资源
5.0 默认情况下,Delete请求是无法提交表单数据的,需要在web.xml中添加过滤器解决
- <!-- 将POST请求转化为DELETE或者是PUT 要用_method指定真正的请求参数 -->
- <filter>
- <filter-name>HiddenHttpMethodFilter</filter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
5.1删除资源的代码实现
- /**
- * 删除资源
- *
- * @param user
- * @return
- */
- @RequestMapping(method = RequestMethod.DELETE)
- public ResponseEntity<Void> deleteUser(
- @RequestParam(value = "id", defaultValue = "0") Long id) {
- try {
- this.userService.deleteById(id);
- // 204
- return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 出现异常,服务器内部错误
- // 500
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
- null);
- }
5.2 删除资源的测试
6、完整的实现代码
- 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 cn.itcast.user.pojo.User;
- import cn.itcast.user.service.UserService;
- @RequestMapping("new/user")
- @Controller
- public class RestUserController {
- @Autowired
- private UserService userService;
- @RequestMapping(value = "{id}", method = RequestMethod.GET)
- public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
- try {
- User user = this.userService.queryUserById(id);
- if (null == user) {
- // 资源不存在 404
- return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
- }
- // 资源存在 200
- return ResponseEntity.status(HttpStatus.OK).body(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.userService.saveUser(user);
- // 201
- return ResponseEntity.status(HttpStatus.CREATED).build();
- } catch (Exception e) {
- 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.userService.updateUser(user);
- // 204
- 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 {
- this.userService.deleteById(id);
- // 204
- return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 出现异常,服务器内部错误
- // 500
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
- null);
- }
- }
7、源码下载