发表于: 2019-12-09 22:47:24
1 1378
今天做了什么
1.将demo改成restful服务
重写一个RestfulController
@Controller
@RequestMapping("/restful/student")
public class RestfulController {
@Autowired
StudentService studentService;
//查询所有,return 所有
@ResponseBody
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Student> listAll() {
List<Student> students = studentService.queryAllStudent();
return students;
}
//根据id查询
@ResponseBody
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public ResponseEntity<Student> queryById(@PathVariable("id") long id) {
try{
Student student =this.studentService.queryById(id);
if(null == student){
//404
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
//200
return ResponseEntity.ok(student);
}catch(Exception e){
e.printStackTrace();
}
//500错误
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 新增用户
*
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> saveUser(Student student) {
try {
this.studentService.addStudent(student);
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);
}
/**
* 更新用户资源
*
*
*/
@RequestMapping(value = "{id}",method = RequestMethod.PUT)
public ResponseEntity<Void> updateUser(Student student) {
try {
this.studentService.updateStudent(student);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 删除用户资源
*
*
*/
@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@RequestParam(value = "{id}") Long id) {
try {
if (id.intValue() == 0) {
// 请求参数有误
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
this.studentService.deleteStudentById(id);
// 204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
在web.xml中配置过滤器,将POST请求转化为DELETE或者是PUT 要用_method指定真正的请求方法
2.通过validation配置pojo实体类属性进行参数校验(未完成)
如
在springmvc的配置文件里配置
不知道怎么做错误回显,明天再弄
遇到的问题:
明天要做什么
把项目改好(参数校验部分),
学习使用nginx
使用git在服务器上部署项目
评论