SpringBoot项目ResponseEntity返回总结
ResponseEntity进行CRUD
这题是使用ResponseEntity进行返回值,不包含通用异常处理。
1、查询
@GetMapping("queryall")
public ResponseEntity<List<Announceconsultsituation>> queryAll(){
try {
List<Announceconsultsituation> announceconsultsituations = announceconsultsituationService.queryList(new Announceconsultsituation());
if(CollUtil.isEmpty(announceconsultsituations)){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
return ResponseEntity.ok(announceconsultsituations);
}catch (Exception e){
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
@GetMapping("queryone")
public ResponseEntity<List<Announceconsultsituation>> queryone(){
try {
Announceconsultsituation announceconsultsituation = announceconsultsituationService.queryOne(id);
if(announceconsultsituation==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
return ResponseEntity.ok(announceconsultsituation);
}catch (Exception e){
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
2、添加
@PostMapping("add")
public ResponseEntity<Void> add(Announceconsultsituation announceconsultsituation){
try {
int count = announceconsultsituationService.add(announceconsultsituation);
// 添加数据成功,返回201
if(count == 1){
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}catch (Exception e){
e.printStackTrace();
}
// 执行失败或者发生异常,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
3、修改
@PutMapping("update")
public ResponseEntity<Void> update(Announceconsultsituation announceconsultsituation){
try {
int count = announceconsultsituationService.update(announceconsultsituation);
// 正常更新数据,返回204
if(count == 1){
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}catch (Exception e){
e.printStackTrace();
}
// 执行失败或者发生异常,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
4、删除
@DeleteMapping("del/{id}")
public ResponseEntity<Void> delete(@PathVariable("id") Integer id){
try {
//参数没有或者格式错误,返回400
if(id == null){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
int count = announceconsultsituationService.delete(id);
// 正常删除,返回204
if(count == 1){
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}catch (Exception e){
e.printStackTrace();
}
// 执行失败或者发生异常,返回500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
自定义ResponeBean进行CRUD
自定义的ResponseBean
public class RespBean {
private Integer status;
private String msg;
private Object obj;
public static RespBean build() {
return new RespBean();
}
public static RespBean ok(String msg) {
return new RespBean(200, msg, null);
}
public static RespBean ok(String msg, Object obj) {
return new RespBean(200, msg, obj);
}
public static RespBean error(String msg) {
return new RespBean(500, msg, null);
}
public static RespBean error(String msg, Object obj) {
return new RespBean(500, msg, obj);
}
private RespBean() {
}
private RespBean(Integer status, String msg, Object obj) {
this.status = status;
this.msg = msg;
this.obj = obj;
}
public Integer getStatus() {
return status;
}
public RespBean setStatus(Integer status) {
this.status = status;
return this;
}
public String getMsg() {
return msg;
}
public RespBean setMsg(String msg) {
this.msg = msg;
return this;
}
public Object getObj() {
return obj;
}
public RespBean setObj(Object obj) {
this.obj = obj;
return this;
}
}
1、查询
@GetMapping("/")
public List<JobLevel> getAllJobLevels() {
return jobLevelService.getAllJobLevels();
}
2、增加
@PostMapping("/")
public RespBean addJobLevel(@RequestBody JobLevel jobLevel) {
if (jobLevelService.addJobLevel(jobLevel) == 1) {
return RespBean.ok("添加成功!");
}
return RespBean.error("添加失败!");
}
3、更新
@PutMapping("/")
public RespBean updateJobLevelById(@RequestBody JobLevel jobLevel) {
if (jobLevelService.updateJobLevelById(jobLevel) == 1) {
return RespBean.ok("更新成功!");
}
return RespBean.error("更新失败!");
}
4、删除
@DeleteMapping("/{id}")
public RespBean deleteJobLevelById(@PathVariable Integer id) {
if (jobLevelService.deleteJobLevelById(id) == 1) {
return RespBean.ok("删除成功!");
}
return RespBean.error("删除失败!");
}