好的,我已经阅读了一些教程,我可以创建这样的东西
public class RestService<T, I> extends RestController{
T get(I id);
List<T> getAll();
T create();
T patch(I id);
T put(I id);
T delete(I id);
}
然后我可以继续创建一些实际的服务。我需要使用RequestMapping(&#34; / service / {id}&#34;)注释get方法并使用PathVariable。
@RequestMapping("/service/{id}")
public T get(@PathVariable id) {
...
}
@RequestMapping("/service/")
public List<T> getAll() {
...
}
正如你所看到的,我正在重复&#34; service&#34;一遍又一遍地。我怎么能做这样的事呢。
class RestService extends RestController() {
private String name; // use this name in request mapping somehow.
}
答案 0 :(得分:1)
您可以在类上定义路径,并在其方法上定义相对路径。例如:
@Controller
@RequestMapping("/service")
public class MyController {
@RequestMapping("/{id}")
public T get(@PathVariable id) {
...
}
@RequestMapping("/")
public List<T> getAll() {
...
}
}
答案 1 :(得分:0)
您可以将@RequestMapping
应用到课程中:
@RequestMapping("/service")
public class RestService extends RestController() {
@RequestMapping("/{id}")
public T get(@PathVariable id) {
...
}
}
答案 2 :(得分:0)
一旦Spring容器在启动时初始化应用程序上下文,它将扫描所有注释了注释的类@Service, @Controller
等。
因此,一旦所有请求映射都被初始化并映射到控制器方法,它就不能再次重新映射。
为了使它更具动态性,我们总能做到这样的事情:
@Controller
public class MyRestController {
@RequestMapping("/{service}/{id}")
public T get(@PathVariable service,@PathVariable id) {
...
}
}
我们可以根据需要改变服务和id Pathvariable。
我正在写这个答案,因为我无法在user1685095声明中添加评论。