Reputation: 950
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
.
Upvotes: 2
Views: 7712
Reputation: 2023
Three things may have problems
spring-boot-starter-tomcat
& spring-boot-starter-web
libs could be missing.com.company.base
package because of you defined scanBasePackages
.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);
}
}
Upvotes: 3
Reputation: 461
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.
Upvotes: 0
Reputation: 13083
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);
}
}
Upvotes: 0
Reputation: 186
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.
Upvotes: 0