Search code examples
javaspring-bootapiresttomcat

Java controller always return a 404 no matter what I do


I was following an example from the spring boot documentation and the java controller always returns a 404 for some reason

This is what I tried

package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
  @Autowired // This means to get the bean called userRepository
         // Which is auto-generated by Spring, we will use it to handle the data
  private UserRepository userRepository;

  @PostMapping(path="/add") // Map ONLY POST Requests
  public @ResponseBody String addNewUser (@RequestParam String name
      , @RequestParam String email) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request

    User n = new User();
    n.setName(name);
    n.setEmail(email);
    userRepository.save(n);
    return "Saved";
  }

  @GetMapping(path="/all")
  public @ResponseBody Iterable<User> getAllUsers() {
    // This returns a JSON or XML with the users
    return userRepository.findAll();
  }
}

which is the example given by spring boot here https://spring.io/guides/gs/accessing-data-mysql/

Then I also tried

@RestController
@RequestMapping("/demo")
public class GreetingClass {

    @GetMapping("/greeting")
    public String getGreeting() {
        return "hello";
    }
}

.pom file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.braintobytes</groupId>
    <artifactId>Finance_microservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Finance_microservice</name>
    <description>Finance service</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

I know this is weird, I set up everything as instructed on the page but apparently nothing works, and I also checked other questions similar to this none of them answer my question.

It does seem that the tomcat server is hit, because it says when I make a call

o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms

but then I get with this call curl 'http://localhost:8080/demo/greeting' and with 'http://localhost:8080/demo/all'

curl : The remote server returned an error: (404) Not Found.

Logs (the file path is removed on the first line):

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-08-31 08:19:16.508  INFO 20708 --- [           main] c.b.f.FinanceMicroserviceApplication     : Starting FinanceMicroserviceApplication on DESKTOP-A65224I with PID 20708 ()
2020-08-31 08:19:16.510  INFO 20708 --- [           main] c.b.f.FinanceMicroserviceApplication     : No active profile set, falling back to default profiles: default
2020-08-31 08:19:17.096  INFO 20708 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-31 08:19:17.101  INFO 20708 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-31 08:19:17.101  INFO 20708 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-31 08:19:17.150  INFO 20708 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-31 08:19:17.150  INFO 20708 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 618 ms
2020-08-31 08:19:17.465  INFO 20708 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-31 08:19:17.675  INFO 20708 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-31 08:19:17.681  INFO 20708 --- [           main] c.b.f.FinanceMicroserviceApplication     : Started FinanceMicroserviceApplication in 1.328 seconds (JVM running for 1.835)
2020-08-31 08:20:21.893  INFO 20708 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-31 08:20:21.893  INFO 20708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-08-31 08:20:21.905  INFO 20708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms

Response:

{
    "timestamp": "2020-08-31T13:20:21.930+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/demo/greeting"
}

This works if I do this:

https://github.com/BraintoByte/Test_App_Stack/tree/master/demo

Different example:

I have attached a different example with hirarchy exactly the same but business logic omitted:

Code in controller:

package com.braintobytes.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.braintobytes.data.Currency;
import com.braintobytes.data.repository.CurrencyRepository;

@RestController
@RequestMapping("/demo")
public class CurrencyController {
    @Autowired
    private CurrencyRepository currencyRepository;
    
    @GetMapping("/currency")
    public String getCurrency() {
        return "currency";
    }
}

Project hierarchy:

enter image description here

Same exact pom and in application.properties:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword

Solution

  • Alright so none of the suggestions above worked, we were on the right path though.

    Changing the structure as so worked:

    enter image description here

    There was also a pathing problem in the application.properties and thus I changed as so:

    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}

    To:

    jdbc:mysql://localhost

    And finally:

    package com.braintobytes.finance.data.repository;
    
    import org.springframework.data.repository.CrudRepository;
    
    import com.braintobytes.finance.data.Currency;
    
    public interface CurrencyRepository extends CrudRepository<Currency, Integer> {
    
    }
    

    Woah!