dushkin
dushkin

Reputation: 2101

Spring boot 2: Fail to reach controller method

I am having some spring boot rest tutorial. I fail to reach the controller method when I call:

http://localhost:8090/customers/stam

Tomcat log:

o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8090 (http) with context path ''

t.s.SpringbootRestDemoApplication : Started SpringbootRestDemoApplication in 2.696 seconds (JVM running for 4.042)

The response I get:

{
    "timestamp": "2019-06-02T12:25:03.400+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/customers/stam"
}

Can you assist?

enter image description here

package ttt.springboot_rest_demo;
import ...

@SpringBootApplication
@ComponentScan({"springboot_rest_demo.controller", "springboot_rest_demo.data"})
public class SpringbootRestDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRestDemoApplication.class, args);
    }

}

package ttt.springboot_rest_demo.controller;    

import ...

@RestController
@RequestMapping("/customers")
public class CustomerController {

    @RequestMapping(value = "/stam", method = RequestMethod.GET)
    public ResponseEntity < Customer > getCustomer() {
        return new ResponseEntity < >(new Customer(), HttpStatus.OK);
    }
}

package ttt.springboot_rest_demo.data;

public class Customer {

    private String name;
    private int age;
    private String email;
    private Long id;

    //getters and setters
}

This is only a part of the project. I use also a service class but because I have failed, I a added a simple controller method which doesn't need the service class for now, just to ease the example.

Upvotes: 0

Views: 220

Answers (1)

helospark
helospark

Reputation: 1460

Your ComponentScan is incorrect, please check the packages (these package names do not exists):

@ComponentScan({"springboot_rest_demo.controller", "springboot_rest_demo.data"})

Your controller is in ttt.springboot_rest_demo.controller package. Change the package name in the ComponentScan to this package.

@ComponentScan({"ttt.springboot_rest_demo.controller", "springboot_rest_demo.data"})

Alternatively just leaving out the ComponentScan will also work for you, because then you will rely on the default behaviour of Spring Boot to scan all packages under the SpringBootApplication.

Note that if your controller is not a managed bean (for example not scanned by ComponentScan) any Spring annotation you add (like RequestMapping, RestController) is ignored.

Upvotes: 2

Related Questions