Nanor Nanor - 2 months ago 7
Java Question

Spring producing 404 for valid URL

I'm building a simple Spring application with the goal of returning "Hello world" as a foundation to build upon. I set up a clean project last night following this guide which worked and now I'm trying to bring an already existing project to the same functionality.

I have two files called

ApplicationConfig.java
and
Controller.java
tasked with returning a string when a certain URL is hit. When I visit
localhost:8080
it renders my
index.html
with a link to the URL I wish to return a string at. When I visit the URL
localhost:8080/home/greet
it returns a 404.

My
ApplicationConfig.java
:

package application;

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

@SpringBootApplication
public class ApplicationConfig {

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

}


and my
Controller.java


package controller;

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

@RestController
@RequestMapping("/home")
public class Controller {

@RequestMapping("/greet")
public String greeting() {
return "Hello";
}
}


As far as I see
/home/greet/
should produce a page that just reads "Hello" but this isn't the case. What is the issue?

Here is my
pom.xml
and what my project structure looks like, should they be relevant.

<?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>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


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

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>


structure

Answer

Your entrypoint class ApplicationConfig.java is in application package and Controller.java is in controller package.

SpringBoot scans for Spring components in the package( and sub-packages) where EntryPoint class is.

So move your controller to application or any nested package under application.