package com.leyou.controller;
import com.leyou.pojo.Category;
import com.leyou.service.CategoryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/category")
public class CategoryController {
@Resource
private CategoryService categoryService;
@GetMapping("list")
public ResponseEntity <List<Category>> queryAllCategoryByPid(@RequestParam(name = "pid",defaultValue = "0") Long pid){
if(pid==null || pid<0){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
List<Category> categories = categoryService.queryAllCategoryByPid(pid);
if(CollectionUtils.isEmpty(categories)){
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok(categories);
}
@GetMapping("bid/{bid}")
public ResponseEntity<List<Category>> queryCategoryByBId(@PathVariable("bid") Long bid){
List<Category> categories = categoryService.queryAllCategoryByPid(bid);
if (categories==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok(categories);
}
}
- 可以看出ResponseEntity是自带的
- **ResponseEntity.status(HttpStatus.NOT_FOUND).build();**里面有标准的状态码,404,400,200
- ResponseEntity.ok(categories);可以把成功后的数据携带到前端。
|