0

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.

8
  • Did you place your controller in a package so it is component scanned? Commented Jun 2, 2019 at 12:28
  • @helospark Sure. You can see it in ComponentScan annotation. I think that otherwise the application wouldn't load, would it? Commented Jun 2, 2019 at 12:30
  • It would load without it, unless you are trying to inject the bean. What package is your CustomerController in? Also have you set any context path? Commented Jun 2, 2019 at 12:31
  • @helospark I edited the question. Please see if it can enlighten the problem. Commented Jun 2, 2019 at 12:37
  • So CustomerController is in the default package (has no package declaration)? Commented Jun 2, 2019 at 12:38

1 Answer 1

2

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.

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

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.