简体   繁体   中英

Does @PathVarible work even when declared in an interface?

I've made some abstraction of Controller using an interface.

-- The problem is that @PathVariable doesn't seem to work on implementation.

-- Please help. Here 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

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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