0

So I've got a simple spring boot app, @SpringBootApplication, a stub @Configuration and a @RestController all in the same package. Spring-boot-web-starter is there and the webserver comes up fine, actuator endpoints and all. But I cannot for the life of me get the app to pick up the @RestControllers.

enter image description here

Main.class:

package com.iglossolalia.munin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
 public static void main(String[] args) {
 SpringApplication.run(MuninContext.class, args);
 }
}

MuninContext.class:

package com.iglossolalia.munin;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
public class MuninContext {
}

MuninService.class:

package com.iglossolalia.munin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MuninService {
 private static final Logger LOG = LoggerFactory.getLogger(MuninService.class);
 public MuninService() {
 LOG.info("Started MuninService");
 }
 @GetMapping("/health")
 public String healthCheck() {
 return "pong";
 }
}

Tried explicitly adding the rest controller to the component scan with no luck there.

4
  • 1
    What error you are getting? Commented Jan 12, 2023 at 20:17
  • 404, plus I can see in the actuator the RestController isn't being bound. Commented Jan 12, 2023 at 20:25
  • Is the bean being loaded? Do you see "Started MuninService" in your log output? How do you try to access your endpoint? Commented Jan 12, 2023 at 21:03
  • The answer from @timofeevle is right. You exclude @ComponentScan from @SpringBootApplication by using your custom bootstrap class. Commented Jan 13, 2023 at 2:27

2 Answers 2

2

You have no @ComponentScan annotation in your MuninContext. Actually you can write SpringApplication.run(Main.class, args) in main method as Spring Initializr generate by default, you don't really need your context, because @SpringBootApplication work as configuration and contains @EnableAutoConfiguration, @ComponentScan, and some other annotations. Otherwise, as you pass your config class as argument in SpringApplication.run method, annotation @SpringBootApplication in Main class has no effect

answered Jan 12, 2023 at 22:04
Sign up to request clarification or add additional context in comments.

Comments

0

In the MuninService RestController you have defined only a constructor() MuninService(),and added a /health endpoint supporting the actuator, but there are no user defined endpoints defined in the Controller, so even though the service started without issues(as per your comment) there are no endpoints to test the status of Controller.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.