user6882156
user6882156

Reputation:

SpringBoot 1.5.10.RELEASE: REST service keeps geting 404

I'm seeing dozens of similar posts but none of the proposed solutions worked for me. I have a WAR with the following structure:

The folder "controller" contains a REST controller having the following code:

@RestController
@RequestMapping("/api")
public class HmlRestController
{
  ...........
  @RequestMapping(value = "/myResource/", method = RequestMethod.POST)
  public ResponseEntity<?> myResource(@RequestBody HmlEvent hmlEvent)
  {
    ..........
  }
}

The application.properties file has the following entry:

server.context-path=/hml

Running the WAR in Tomcat embedded and going to localhost:8080/hml displays the index.html file but trying to POST to localhost:8080/hml/api/myResource returns HTTP 404. If I move this code into the HmlApplication class, like this:

@SpringBootApplication
@RestController
@RequestMapping("/api")
public class HmlApplication
{
  public static void main(String[] args)
  {
    SpringApplication.run(HmlApplication.class, args);
  }

  @RequestMapping(value = "/myResource/", method = RequestMethod.POST)
  public ResponseEntity<?> myResource(@RequestBody HmlEvent hmlEvent)
  {
    ..........
  }`enter code here`
}

then it works as expected. Could someone please shed some lights here and let me know what is going on here ? Many thanks in advance, Nicolas

Upvotes: 0

Views: 62

Answers (2)

user6882156
user6882156

Reputation:

I don't know exactly what happened but restarting everything solved the issue. The only thing I could think at is that, during the IntelliJ session, some message saying something about several Spring contexts has been displayed and I choose the default action. But I'm not any more sure. So for now, I'm closing this issue. Kind regards, Nicolas

Upvotes: 0

KenCoenen
KenCoenen

Reputation: 513

By using Spring Boot and its @SpringBootApplication, you automatically enable component scanning (see @ComponentScan on the @SpringBootApplication annotation).

You didn't mention your package structure, but your other package hierarchies should be below your main app with the @SpringBootApplication annotation, HmlApplication in your case.

You can add packages explicitly by adding an extra @ComponentScan annotation to your application class.

@ComponentScan(“my.package”)
@SpringBootApplication
public class HmlApplication {

Upvotes: 2

Related Questions