我们利用搭建SSM环境整合通用Mapper和PageHelper这篇博文搭建的SSM环境来讲解SpringMVC实现RESTful服务。
查询资源
测试:
新增资源
第一次测试:
这个时候显示新增成功了。我们再来看看数据库中是否存在这条数据了。
我们这次在重新提交一次:
更新资源
默认情况下,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>
测试:
删除资源
需要在web.xml中添加过滤器解决DELETE请求无法提交表单数据的问题:
<!--
将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>
测试:
Controller 代码
package my.ssm.controller;
import my.ssm.pojo.User;
import my.ssm.service.UserService;
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;
@RequestMapping("new/user")
@Controller
public class NewUserController {
@Autowired
private UserService userService;
// 根据用户id查询用户信息
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
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);
return ResponseEntity.ok(user);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 新增用户
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> saveUser(User user) {
try {
Boolean bool = this.userService.saveUser(user);
if (bool) {
// 新增成功,响应201
return ResponseEntity.status(HttpStatus.CREATED).build();
}
} catch (Exception e) {
e.printStackTrace();
}
// 新增失败,响应500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
/**
* 更新用户
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<Void> updateUser(User user) {
try {
Boolean bool = this.userService.updateUser(user);
if (bool) {
// 更新成功,响应204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
} catch (Exception e) {
e.printStackTrace();
}
// 新增失败,响应500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
/**
* 删除用户
*
* @param id
* @return
*/
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
try {
if (id.longValue() == 0) {
// 没有传递参数,响应状态码400
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
Boolean bool = this.userService.deleteUser(id);
if (bool) {
// 删除成功,响应204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
} catch (Exception e) {
e.printStackTrace();
}
// 删除失败,响应500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
Service 代码
package my.ssm.service;
import java.util.List;
import my.ssm.bean.EasyUIResult;
import my.ssm.mapper.UserMapper;
import my.ssm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public EasyUIResult queryUserList(Integer page, Integer rows) {
// 设置分页参数
PageHelper.startPage(page, rows);
// 查询User数据
Example example = new Example(User.class);
example.setOrderByClause("updated DESC"); // 设置排序条件
List<User> users = this.userMapper.selectByExample(example);
// 获取分页后的信息
PageInfo<User> pageInfo = new PageInfo<User>(users);
return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
}
public User queryUserById(Long id) {
return this.userMapper.selectByPrimaryKey(id);
}
public Boolean saveUser(User user) {
return this.userMapper.insert(user) == 1;
}
public Boolean updateUser(User user) {
return this.userMapper.updateByPrimaryKeySelective(user) == 1;
}
public Boolean deleteUser(Long id) {
return this.userMapper.deleteByPrimaryKey(id) == 1;
}
}