gpt4 book ai didi

java - 实现具有相同功能的 Controller 的最佳实践是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:11 26 4
gpt4 key购买 nike

我有一个简单的 WebApp,它有两个表 ClothesShoes
创建 Controller 的最佳实践是什么,因为它们都有 CRUD方法。
我有两个这样的 Controller :

@Controller
@RequestMapping("/shoes")
@Component
public class ShoesController {
private ShoesService shoesService;

@Autowired
public void setShoesService(ShoesService shoesService) {
this.shoesService = shoesService;
}

public ShoesService getShoesService() {
return shoesService;
}

@RequestMapping(value = "/view/all", method = RequestMethod.GET)
public @ResponseBody
List<Shoes> getAllShoes(){
return shoesService.getAllShoes();
}

@RequestMapping(value = "/byId/{id}", method = RequestMethod.GET)
public @ResponseBody
Shoes getShoesById(@PathVariable ("id") Integer id, Shoes shoes){
//TODO remove hardcoded values
return shoesService.getShoesByID(id, shoes);
}

@RequestMapping(value = "/view/{columnName}={value}", method =
RequestMethod.GET)
public @ResponseBody
List<Shoes> getByColumnValue(@PathVariable ("columnName") String colunmName,
@PathVariable("value") String columnValue, Shoes shoes){
//TODO remove hardcoded values
return shoesService.getByColumnValue(colunmName, columnValue, shoes);
}

@RequestMapping(value = "/edit/id={id}", method = RequestMethod.GET)
public @ResponseBody
Shoes update(@PathVariable ("id") Integer id, Shoes shoes){

shoes = getShoesById(id, shoes);
shoes.setShoesSeason("SPRINGGG");
shoesService.updateShoes(shoes);
return shoes;
}

@RequestMapping(value = "/delete/{id}", method =RequestMethod.GET)
public @ResponseBody
List<Shoes> delete(@PathVariable ("id") Integer id, Shoes shoes){
shoes = getShoesById(id, shoes);
shoesService.delete(shoes, id);
return getAllShoes();
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public @ResponseBody
List<Shoes> add (Shoes shoes){
shoes = new Shoes(333666, "Blue", "Autumn", "Noski", "URL");
shoesService.add(shoes);
return shoesService.getAllShoes();
}
}

Clothes Controller 具有相同的实现。
实现具有相同功能的 Controller 的最佳实践是什么?

最佳答案

我认为好的和简单的解决方案是使用 Spring Data REST .您还可以创建抽象 Controller ,它可以根据您要使用的类型进行参数化。

关于java - 实现具有相同功能的 Controller 的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46644883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com