简体   繁体   English

@PathVarible即使在接口中声明也可以工作吗?

[英]Does @PathVarible work even when declared in an interface?

I've made some abstraction of Controller using an interface. 我已经使用接口对Controller进行了一些抽象。

-- The problem is that @PathVariable doesn't seem to work on implementation. -问题是@PathVariable似乎无法在实现中使用。

-- Please help. - 请帮忙。 Here Controller.java 这里Controller.java

public interface Controller<ENTITY extends Entity>
{

    @RequestMapping(value = "/", method = RequestMethod.GET)
    default public void process(final Model model)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    default public void processNew(final Model model)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/{id}/show", method = RequestMethod.GET)
    default public void processShow(final Model model, @PathVariable Long id)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    default public void processEdit(final Model model, @PathVariable Long id)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/{id}/drop", method = RequestMethod.GET)
    default public void processDrop(final Model model, @PathVariable Long id)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    default public void processCreate(@Valid @ModelAttribute ENTITY entity, BindingResult result, SessionStatus sessionStatus)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    default public void processUpdate(@Valid @ModelAttribute ENTITY entity, BindingResult result, SessionStatus sessionStatus)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    default public void processDelete(@Valid @ModelAttribute ENTITY entity, BindingResult result, SessionStatus sessionStatus)
    {
        throw new UnsupportedOperationException("Operation not yet supported !");
    }

}

...and here ProjetController.java & requests where @PathVariable doesn't work ...这里是ProjetController.java并请求@PathVariable不起作用的地方

@Controller
@RequestMapping("/projects")
public class ProjectController implements Controller<Project>
{
    ...
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    ...
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    ...
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    ...
}

For what concerns the mapping not working in an implementation I wouldn't know, everything appears fine. 对于映射在我不知道的实现中不起作用的问题,一切似乎都很好。

Regarding the annotation on the interface, they don't serve any purpose. 关于界面上的注释,它们没有任何作用。 Mappings are obsolete as annotations that you've set on the interfaces are not applied to the implementing class. 映射已过时,因为您在接口上设置的注释不会应用于实现类。

On the other hand, they are not interfering neither, as mappings are always mapped against a concrete implementing method, which you should see in your logs at start-up if you set on debug level. 另一方面,它们也不会干扰,因为映射始终针对具体的实现方法进行映射,如果在调试级别进行设置,则应在启动时在日志中看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM