Failing to reach RESTful service with Spring Boot App

Multi tool use
Failing to reach RESTful service with Spring Boot App
I'm trying to run a simple Spring Boot application on Wildfly 13.0.0, but I always get a 404 error when trying to reach any REST urls.
Here's my app class:
package com.application.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class ExampleApp extends SpringBootServletInitializer{
public static void main(String args) throws Exception{
SpringApplication.run(ExampleApp.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<ExampleApp> applicationClass = ExampleApp.class;
}
@RestController
class HelloController {
@RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {
return "Hi " + name + " !";
}
}
And here's my pom.xml:
<?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.application</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>application-example</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The .war is deployed correctly and I can reach a html index page I created if I go to localhost:8080/application-example/index.html but if I try localhost:8080/application-example/hello or localhost:8080/application-example/hello/myName or even the original name with 0.0.1 Snapshot at the end I always get a 404.
Of course this also happens for RestController classes inside inner packages but in the above example we are in the same package so I don't think it is a visibility/scanning issue.
Here's my application.properties as requested:
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.datasource.url=jdbc:mysql://localhost:3306/example
spring.datasource.username=root@localhost
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
server.servlet.contextPath=/application-example
Am I missing something? If you need further details please ask away.
/hello/{name}
/hello
/hello
localhost:8080/application-example/hello/myName
hey @IvoVidovic, yes I also tried that one, sorry for not specifying it in my original question. Doesn't work.
– Andrew
1 hour ago
Please post your application properties file too. I am just wondering if you have server context path rightly setup.
– Amith Kumar
56 mins ago
@AmithKumar Just posted it
– Andrew
53 mins ago
You deployed your application as "application-example" on your Tomcat and added
server.servlet.contextPath=/application-example
specifically to the application properties, is this correct? I am not 100 % sure, since I cannot deploy it on a tomcat currently, but could you try out: localhost:8080/application-example/application-example/hello/myName
?– Ivo Vidovic
24 mins ago
server.servlet.contextPath=/application-example
localhost:8080/application-example/application-example/hello/myName
1 Answer
1
As described in the Spring Docs, you are missing on below:
A popular subject is that many people still wish to generate WAR files to be deployed inside containers. Both of these plugins support that as well. Essentially, you have to reconfigure your project to produce a WAR file and declare the embedded container dependencies as "provided". This ensures that the relevant embedded container dependencies aren’t included in the WAR file.
To build a war file that is both executable and deployable into an external container, you need to mark the embedded container dependencies as “provided”, as shown in the following example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
NOTE: You don't need exclusions.
UPDATE
I see your issue now, you have defined Controller class as non-public inside Spring Application class. Move controller to new file as public class.
Still doesn't work
– Andrew
9 mins ago
check my update
– Amith Kumar
1 min ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
your requestmapping suggests
/hello/{name}
, so did you only try to send a request to/hello
? Do you have a mapping to/hello
as well? If not, then trylocalhost:8080/application-example/hello/myName
– Ivo Vidovic
1 hour ago