Kristof
Kristof

Reputation: 1734

Spring RestController ambiguous mapping error

I'm setting up a RestController in Spring, and I have an ambiguous mapping issue. I don't see how the last two methods are ambiguous, because the request mappings and method names are different. When I remove the method specification from the last method, the issue isn't there anymore.

This is my controller:

@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    @RequestMapping(name = "/tag/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<TagList> getTagList() {
        TagList result = new TagList(tagService.list());
        return new ResponseEntity<TagList>(result, HttpStatus.OK);
    }

    @RequestMapping(name = "/tag/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> addTag(@RequestBody AlterTagForm form) {
        try {
            tagService.addTag(form.getArticleId(), form.getTagName());
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        } catch (EntityNotFoundException ex) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(name = "/tag/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> removeTag(@RequestBody AlterTagForm form) {
        try {
            tagService.removeTag(form.getArticleId(), form.getTagName());
            return new ResponseEntity<>(HttpStatus.ACCEPTED);
        } catch (EntityNotFoundException ex) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
}

which results in this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'tagController' bean method 
public org.springframework.http.ResponseEntity<?> com.example.article.controller.TagController.removeTag(com.example.admin.form.AlterTagForm)
to {[],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'tagController' bean method
public org.springframework.http.ResponseEntity<?> com.example.article.controller.TagController.addTag(com.example.admin.form.AlterTagForm) mapped.

Upvotes: 0

Views: 3605

Answers (2)

user3145373 ツ
user3145373 ツ

Reputation: 8156

Try the RequestMapping using value property.

Value property should be unique otherwise its gonna throw an exception. Right now in your code both method redirect to / by default.

Upvotes: 2

approxiblue
approxiblue

Reputation: 7122

In your @RequestMapping annotations you should set your paths using value properties. The value property determines the path your methods should handle, while the name property is just for identifying the mapping in the Spring environment.

Your methods addTag() and removeTag() are currently both mapped to the index path of the controller (/), and since they are similar in every way (method, produces, argument) except the name, Spring cries foul.

Upvotes: 3

Related Questions