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>
资源类:
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");
}
}
应用类:
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类:
package com.mycomp.spring.springboot.config;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
public class Appconfiguration {
}
当我运行该应用程序时,它运行良好,但是当我按下http://localhost:port/bankname时,它会给我 404 。
但是,当我像这样更改我的应用程序类时,并没有得到 404 ,并且资源类按预期返回了 bankname 。
@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);
}
}
关于我为什么通过创建实例而不是从静态run方法开始初始化spring应用程序时为什么得到 404 的想法?
答案 0 :(得分:0)
尝试对资源文件使用以下代码:
@Controller
public class BankResource {
// Some Codes ...
}
将@RestController
更改为@Controller
。