deve deve - 1 month ago 7
reST (reStructuredText) Question

Can't access REST Controller with Spring Boot autoconfiguration

I am trying to create a simple REST service with Spring Boot following this tutorial. The "Hello World" html file from the webapp folder (

index.html
) is opening on
http://localhost:8080/my-rest-app/
(I created a Maven-Web-App because I want to have a "Welcome Page" for the Service). But if I try to access the REST service on
http://localhost:8080/my-rest-app/user
I get a 404 message.

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>service</groupId>
<artifactId>my-rest-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>my-rest-app</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Application.java:

package service.my.rest.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
//@ComponentScan({"service.my.rest.app", "service.my.rest.app.controller"})
//@ComponentScan(basePackageClasses = UserController.class)
public class Application {

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

}


UserController.java:

package service.my.rest.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

@RequestMapping(method = RequestMethod.GET)
public String getUser() {
return "Hello";
}

}


What am I missing? Is the URL to the service wrong? I read somewhere that the context path of the Spring Boot REST service is always
/
, that would meant that I have to access the service over
http://localhost:8080/my-rest-app/
but this does not working ether (without
index.html
). Changing the context path in the
application.properties
with
server.contextPath=/my-rest-app
and
server.port=8080
also didn't help.

Answer

The problem was the build and deploy process. Thank you @chrylis for the hint.

package service.my.rest.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"service.my.rest.app"})
public class Application extends SpringBootServletInitializer {

    private static Class<Application> application = Application.class;

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

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

}

I had to extend my App to build and deploy a war file, because Spring Boot is actually designed to produce a jar.

Comments