Springboot springapplication gives 404

Asked by At

Pom:

<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.mycomp.spring</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot</name>
  <url>http://maven.apache.org</url>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <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>

Resource class:

 package com.mycomp.spring.springboot.resources;

import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mycomp.spring.springboot.services.BankService;

@RestController
public class BankResource {

    private final Environment env;

    private final BankService bankService;

    public BankResource(Environment env, BankService bankService) {
        this.env = env;
        this.bankService = bankService;
    }

    @RequestMapping("/bankname")
    public String getBankName() {
        System.out.println("REQUEST RECEIVED");
        bankService.getUserAccounts("tets");
        return env.getProperty("app.name");
    }

}

App class:

package com.mycomp.spring.springboot;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;

import com.mycomp.spring.springboot.config.Appconfiguration;
import com.mycomp.spring.springboot.initializers.Appintializer;
import com.mycomp.spring.springboot.listeners.AppListener;

/**
 * 
 *
 */

public class App {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Appconfiguration.class);
        app.addInitializers(new Appintializer());
        app.addListeners(new AppListener());
        app.setWebApplicationType(WebApplicationType.SERVLET);
        System.out.println(Arrays.toString(args));
        app.run(args);

        // SpringApplication.run(App.class, args);

    }
}

AppConfiguration class:

package com.mycomp.spring.springboot.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
public class Appconfiguration {

}

When I run the app, it starts well but when I hit http://localhost:port/bankname it gives me 404.

However when I change my app class like this I am not getting 404 and the resource class returns the bankname as expected.

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        // SpringApplication app = new SpringApplication(Appconfiguration.class);
        // app.addInitializers(new Appintializer());
        // app.addListeners(new AppListener());
        // app.setWebApplicationType(WebApplicationType.SERVLET);
        // System.out.println(Arrays.toString(args));
        // app.run(args);

        SpringApplication.run(App.class, args);

    }
}

Any idea as to why I am getting 404 when I initialize the spring application by creating an instance instead of starting with the static run method?

1 Answers

0
AmirBll On

Try this code for Resource file:

@Controller 
public class BankResource {
    // Some Codes ...
}

Change @RestController to @Controller.