gpt4 book ai didi

java - Spring MVC Controller 继承和路由

转载 作者:IT老高 更新时间:2023-10-28 13:53:54 25 4
gpt4 key购买 nike

在我的 Spring MVC webapp 中,我有一个用于 CRUD 操作的通用 RESTful Controller 。而且每个具体的 Controller 只需要声明一个@RequestMapping,例如/foo。通用 Controller 处理所有对 /foo/foo/{id} 的请求。

但现在我需要编写一个更复杂的 CRUD Controller ,它会获取额外的请求参数或路径变量,例如 /foo/{date}/foo/{id}/{日期}。所以我扩展了我的通用 CRUD Controller 并编写了重载的 fetch(id, date) 方法,该方法将同时处理 {id}{date} .这不是问题。

但我还需要“禁用”从基类派生的 fetch(id) 实现(资源不得在 /foo/{id} 处可用,仅在 /foo/{id}/{date})。我想出的唯一想法是在我的具体 Controller 中重写此方法,将其映射到假 uri 并返回 null。但这看起来很丑陋,因为我们暴露了一些虚假的资源 uri,而不是禁用它。有没有更好的做法?

有什么想法吗?

//My generic CRUD controller
public abstract class AbstractCRUDControllerBean<E, PK extends Serializable> implements AbstractCRUDController<E, PK> {

@RequestMapping(method=RequestMethod.GET)
public @ResponseBody ResponseEntity<E[]> fetchAll() { ... }

@RequestMapping(value="/{id}", method=RequestMethod.GET)
public @ResponseBody ResponseEntity<E> fetch(@PathVariable("id") PK id) { ... }

@RequestMapping(method=RequestMethod.POST)
public @ResponseBody ResponseEntity<E> add(@RequestBody E entity) { ... }

@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public @ResponseBody ResponseEntity<E> update(@PathVariable("id") PK id, @RequestBody E entity) { ... }

@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public @ResponseBody ResponseEntity<E> remove(@PathVariable("id") PK id) { .. }
}

.

//Concrete controller, working with Foo entities
@Controller
@RequestMapping("/foo")
public class FooControllerImpl extends
AbstractCRUDControllerBean<Foo, Long> implements FooController {

//ugly overriding parent's method
@RequestMapping(value="/null",method=RequestMethod.GET)
public @ResponseBody ResponseEntity<Foo> fetch(@PathVariable("id") PK id) {
return null;
}

//new fetch implementation
@RequestMapping(value="/{id}/{date}", method=RequestMethod.GET)
public @ResponseBody ResponseEntity<Foo> fetch(@PathVariable("id") PK id, @PathVariable("date") Date date) { .... }

}

最佳答案

你是不是想用spring实现 Jersey 的资源、子资源类型?这可能不是直接可能的。与其将通用 RESTful 服务声明为 Controller ,不如将其委托(delegate)给他们?

//My generic CRUD Operations
public abstract class AbstractCRUDControllerBean<E, PK extends Serializable> implements AbstractCRUDController<E, PK> {

public ResponseEntity<E[]> fetchAll() { ... }

public ResponseEntity<E> fetch(@PathVariable("id") PK id) { ... }

public ResponseEntity<E> add(@RequestBody E entity) { ... }

public ResponseEntity<E> update(@PathVariable("id") PK id, @RequestBody E entity) { ... }

public ResponseEntity<E> remove(@PathVariable("id") PK id) { .. }
}

并在 Controller 中委托(delegate)。

//Concrete controller, working with Foo entities
@Controller
@RequestMapping("/foo")
public class FooControllerImpl extends
AbstractCRUDControllerBean<Foo, Long> implements FooController {

//we are interested in using fetchall but not others
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody ResponseEntity<Foo> fetch(@PathVariable("id") PK id) {
return fetchAll();
}

//fetch with id and date
@RequestMapping(value="/{id}/{date}", method=RequestMethod.GET)
public @ResponseBody ResponseEntity<Foo> fetch(@PathVariable("id") PK id, @PathVariable("date") Date date) { .... }

}

另外,您也可以根据参数的可用性映射方法,

@RequestMapping(value="/{id}/{date}", params={"param1","param2","!param3"})
public @ResponseBody ResponseEntity<E> customFetch(@PathVariable("id") PK id,
@PathVariable("date") Date date, @RequestParam("param1") String param1,
@RequestParam("param2") String param2) {...}

当 param1 和 param2 存在而 param3 不存在时,此方法映射/foo/id/date。

关于java - Spring MVC Controller 继承和路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387592/

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