Menelaos
Menelaos

Reputation: 26549

Different RequestMapping value within same Controller

There is an existing Controller that I want to add an additional get Method with a slightly modified logic. There is the findAll method and I want to add the getMessages method.

@RestController
@RequestMapping(value = "/options", produces = MediaType.APPLICATION_JSON_VALUE)
public class OptionController {

 ...Definitions etc...

 @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<?> findAll(@PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
        Page<Option> page = optionRepository.findAll(pageable);
        return ok(pagingAssembler.toResource(page));
    }
}

And below the new method:

@RequestMapping(value = "/optionsWelcome", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public ResponseEntity<?> getMessages(@PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
    Page<Option> page = optionRepository.findAll(pageable);
    return ok(pagingAssembler.toResource(page));
}

I am getting 404 for http calls to /optionsWelcome but /options works.

Is it possible to have a controller with mappings for 2 different URLs or do I need to make a second controller?

Upvotes: 0

Views: 54

Answers (1)

jny
jny

Reputation: 8067

/options is the mapping for the entire controller. /options/optionsWelcome will probably work.

You need to move /options mapping to the method.

Upvotes: 1

Related Questions