2

The problem is that my rest controller is not getting called.

@SpringBootApplication(scanBasePackages = { "com.company.base" })
public class Application {

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

@RestController
@RequestMapping("/data")
public class DataController {

    private static final Logger LOGGER = LogManager.getLogger(MethodHandles.lookup().lookupClass());

    @Autowired
    private SusbscriberRepository susbscriberRepository;

    @RequestMapping(value = "/subscribers", method = RequestMethod.GET)
    public HttpEntity<SubscriberResource> findAll() {
     ...
    }

    @RequestMapping(value = "/subscriber/{id}", method = RequestMethod.GET)
    public ResponseEntity<SubscriberResource> findSubscriber(@PathVariable(value = "id") Long id) {
      ...
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.base</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Grenty</name>
    <description>Grenty project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <organization>
        <name>SA Technologies</name>
        <url>www.satechnologies.com</url>
    </organization>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.5.4-Final</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
                <version>1.4.196</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>de.jpdigital</groupId>
                <artifactId>hibernate5-ddl-maven-plugin</artifactId>
                <version>1.0.1-hibernate-5.1.2.Final</version>
                <configuration>
                    <dialects>
                        <param>postgresql9</param>
                    </dialects>
                    <packages>
                        <param>com.company.base.model</param>
                    </packages>
                    <outputDirectory>${project.basedir}/docs/sql</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>gen-ddl</goal>
                        </goals>
                        <phase>process-classes</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

In the Springboot start log, these url are mapped.

I'm sure I'm missing something but I'm not sure what.

Even spring example is not working: gs-actuator-service-master.

4
  • Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Jul 19, 2017 at 23:05
  • Can you elaborate on what happens when you make an HTTP request to one of those endpoints? Do you get a Response? If so, what is the HTTP Status? Also, I don't see the spring-boot-starter-web dependency... That is the most conventional approach. Commented Jul 19, 2017 at 23:08
  • I get whitelabel error page and inside 404 HTTP error. Commented Jul 19, 2017 at 23:44
  • And what about the missing spring-boot-starter-web dependency? If you could share a minimal example app on GitHub that reproduces that problem, that would be helpful. Commented Jul 20, 2017 at 3:37

4 Answers 4

3

Three things may have problems

  1. spring-boot-starter-tomcat & spring-boot-starter-web libs could be missing.
  2. Your controller must under the com.company.base package because of you defined scanBasePackages.
  3. Spring boot Application must run with SpringBootServletInitializer. This case will only required if your run as Java Application, that not required if you are running from mvn command.

Please add libraries & update your Application.java and try again.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

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

1 Comment

In my case, spring-boot-starter-web was missing. Thanks for pointing out that!
0

Remove scanBasePackages = { "com.company.base" } if the controller is under boot app package or sub package. Or include package of main class as well in component scan.

Comments

0

I faced a similar issue. I added spring-mvc dependency.

compile "org.springframework:spring-webmvc:5.1.3.RELEASE"

By default, the main class should be in the root package. I put the main class into a different package, so I had to component scan and add other components to be available.

package root.package1

@SpringBootApplication(scanBasePackages = {"root"})

public class WebAppMain {

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

Comments

0

Move controller class into subpackage of the application class.

like 
    com.company.base for Application class
    com.company.base.controller for DataController class

then spring boot will scan controller automatically.

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.