1

I have Spring MVC project to develop sample REST api. It was running when I deploy app in tomcat. Now, I wanted to update the project with Spring boot. I have created the main spring boot application class which is as below at package: com.abc.demo.config

@EnableAutoConfiguration  // Sprint Boot Auto Configuration
@ComponentScan(basePackages = "com.abc.demo")
@EnableWebMvc
public class Application extends SpringBootServletInitializer {

    private static final Class<Application> applicationClass = Application.class;
    private static final Logger log = LoggerFactory.getLogger(applicationClass);

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }
    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

}

My controller is at com.abc.demo.Controller which is like below:

@RestController
public class MyController {

    static final Logger logger = Logger.getLogger(MyController.class);

    @Inject
    private MyService myService;
    @RequestMapping(value = "/mytask/{task}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
     public int myTask(@PathVariable String task) {
        return myService.service(task);
    }

}

When I deploy the application in local tomcat without adding spring boot, it was deploying and I am able to see the response from the service. (I had web.xml and dispatcher servlet as well while app was running). But, when I added spring boot and run the app from command line, app is running (I have run some threads when app starts, when I can see running). But when I do curl, request is not coming to controller at all and seeing the URI not found log in console as :No mapping found for HTTP request with URI as [/[myservice-name]/myTask Did I miss something here? Appreciate if someone points out the things I missed.

4 Answers 4

2

Work with the framework not around it. Move your Application to com.abc.demo (or the actual root package of your project). then remove most of it. All your annotation can be replaced by a simple @SpringBootApplication. Don't create a DispatcherServlet Spring Boot already does that for you as well as configuration Spring MVC.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    private static final Class<Application> applicationClass = Application.class;
    private static final Logger log = LoggerFactory.getLogger(applicationClass);

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }    
}

You can also polish your controller a little as you have @RestController you don't need the @ResponseBody.

@RestController
public class MyController {

    static final Logger logger = Logger.getLogger(MyController.class);

    @Inject
    private MyService myService;

    @RequestMapping(value = "/mytask/{task}", method = RequestMethod.GET, produces = "application/json")        
     public int myTask(@PathVariable String task) {
        return myService.service(task);
    }
}

Finally I would say your url you are using is wrong. By default, when running from the command line, the application is available as the root application i.e. on / and not on /myservice/mytask/{task}.

So try curl http://localhost:8080/mytask/123 instead of including the application name.

If you do want /myservice to be part of the URL you can specify a server.context-path in the application.properties for your application.

server.context-path=/myservice

Then it will be prepended to the URL.

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

1 Comment

Great Explanation Deinum. Thanks
0

No need to pass your [myservice-name].Use url like this

http://localhost:8080/mytask/somevalue

2 Comments

yes. I did that after posting question here which worked.
@user8838 ok great
0

Its a suggestion for people who are getting this problem: Spring boot has already very simple process to create your set-up. Still if you are getting an error where your request is not reaching server

Most probably you would have done a common mistake of declaring your package out of the application context. First Step: your package should be inside your root package example: your controller should be inside com.example.demo if it is your root package.

Second Step: create a simple request which just reach your RequestController like:

@GetMapping("/test")
    public String testServer() {
        return "Server OK ";
    }

So that you can be assured that your request is reaching to this point.

Comments

0

For those who are trying to work with spring-boot in scala I leave it here to save your day. I had a similar situation where controller bean was created but no requests routed to it.

First I used mapping annotations copied from Java examples, like

@GetMapping("/path")

After some suffering I changed it to

@GetMapping(path = Array("/path"))
 

and finally it started working.

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.