0

I have a @RestController below as below. The method getTrain(long) is supposed to be picked up for URLs http://localhost:8080/trains/1 but instead it's picking up getTrains(). The other URLs work fine as expected. I am not sure if I am missing or not understanding something. I also looked at Spring request mapping to a different method for a particular path variable value and it helped a bit.

Requirements: 1. /trains [POST] - add train 2. /trains [GET] - get all trains 3. /trains/{trainId} - get train by id

@RestController
public class TrainController {

    @Autowired
    private TrainService trainService;

    @RequestMapping(headers = { "Accept=application/json" }, method = RequestMethod.POST)
    public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception {

        return trainService.addTrain(trainDto);
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<TrainDto> getTrains() throws Exception {

        return trainService.getTrains();
    }

    @RequestMapping(value = "{trainId:\\d+}", method = RequestMethod.GET)
    public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {

        return trainService.getTrain(trainId);
    }
}
1
  • 1
    have you tried "/{trainId:\\d+} by appending a /? Commented Aug 30, 2018 at 5:48

2 Answers 2

1

You should add value = "" to mapping request. See if this works

@RestController public class TrainController {

    @Autowired
    private TrainService trainService;

    @RequestMapping(value = "/trains",headers = { "Accept=application/json" }, method = RequestMethod.POST)
    public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception {

        return trainService.addTrain(trainDto);
    }

    @RequestMapping(value = "/trains",method = RequestMethod.GET)
    public List<TrainDto> getTrains() throws Exception {

        return trainService.getTrains();
    }

    @RequestMapping(value = "/trains/{trainId}", method = RequestMethod.GET)
    public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {

        return trainService.getTrain(trainId);
    }

}

Or you can do it this way.

@RestController
@RequestMapping(TrainController.REQUEST_MAPPING_URL)
public class TrainController {

       public static final String REQUEST_MAPPING_URL = "/trains";

        @Autowired
        private TrainService trainService;

        @RequestMapping(value = "/",headers = { "Accept=application/json" }, method = RequestMethod.POST)
        public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception {
            return trainService.addTrain(trainDto);
        }

        @RequestMapping(value = "/",method = RequestMethod.GET)
        public List<TrainDto> getTrains() throws Exception {
            return trainService.getTrains();
        }

        @RequestMapping(value = "/{trainId}", method = RequestMethod.GET)
        public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {
            return trainService.getTrain(trainId);
        }

    }
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, this is a train app. So the URL already has trains to it. Doing it like this would result in /trains/trains/{trainId}
I cannot see it in your code. where are you put "/trains" ?
Wow, I tried it and it worked. Can you may be explain why it is not also appending the app name as part of the root url?
Both worked. I forgot that with Spring boot apps, the context path does not default to the app name
@FourtyTwo then you can accept as right answer. The HTTP method parameter has no default – so if we don’t specify a value, it’s going to map to any HTTP request.
0
@RestController
public class TrainController {

    @Autowired
    private TrainService trainService;

    @RequestMapping(headers = { "Accept=application/json" }, method = RequestMethod.POST)
    public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception {

        return trainService.addTrain(trainDto);
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<TrainDto> getTrains() throws Exception {

        return trainService.getTrains();
    }

    @RequestMapping(value = "{trainId}", method = RequestMethod.GET)
    public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {

        return trainService.getTrain(trainId);
    }
}

This would work. The value inside curly braces i.e. trainId should map to PathVariable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.