spring-boot - Wildfly 和 spring boot 出现 "404 - Not found"错误

标签 spring-boot http-status-code-404 wildfly

您好,我正在尝试使用 Eclipse 通过一个非常简单的应用程序来学习 Wildfly 和 springboot。
项目名称是 springboot-test。
包括主方法类在内的所有类都在同一个包中。

Main 方法类称为“App”,其代码如下:

 @SpringBootApplication
 @RestController
 public class App {

    @Autowired
    private Student student;

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

    @RequestMapping("/index")
    public String sayHello()
    {
        return "hello from spring boot" + this.student.showAddress();
    }
}

以下是服务器日志:

11:36:57,281 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 68) WFLYUT0021: Registered web context: '/springboot-test-0.0.1-SNAPSHOT' for server 'default-server'

11:36:57,301 INFO [org.jboss.as.server] (ServerService Thread Pool -- 37) WFLYSRV0010: Deployed "springboot-test-0.0.1-SNAPSHOT.war" (runtime-name : "springboot-test-0.0.1-SNAPSHOT.war")

11:36:57,408 INFO [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server

11:36:57,411 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:8181/management

11:36:57,412 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:8181

11:36:57,412 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 11.0.0.Final (WildFly Core 3.0.8.Final) started in 11393ms - Started 504 of 732 services (353 services are lazy, passive or on-demand)



我访问的网址是: http://localhost:8080/springboot-test/index

最佳答案

由于您使用 wildfly 进行部署,我希望您生成的 war 文件和服务器日志似乎支持我的声明

你有没有 SpringBootServletInitializer 类(class) ???如果不是那是问题

你需要这样的东西

@SpringBootApplication
@RestController
public class ServletInitializer extends SpringBootServletInitializer{

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

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

@Override
public void onStartup(ServletContext servletContext) throws ServletException{
    super.onStartup(servletContext);

     }
@RequestMapping("/index")
public String sayHello(){
        return "hello from spring boot" + this.student.showAddress();
    }
}

我不确定注释 @SpringBootApplication 是否有任何问题和 @RestController在一个类(class)。但我的建议是将其分开进行维护
@RestController
public class Mycontroller{

  @RequestMapping("/index")
  public String sayHello(){
      return "hello from spring boot" + this.student.showAddress();
  }
}

关于spring-boot - Wildfly 和 spring boot 出现 "404 - Not found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48700147/

相关文章:

javascript - Expressjs - 所有路由都是 404

queue - 在 JNDI 查找中找不到 JMS 队列

java - 如何从 JBoss 7 或 WildFly 中部署的应用程序访问 JDK tools.jar javac

jsp - 如何在 Wildfly 10 中预编译 JSP 文件?

Spring 启动@Configurable

java - 如何使用 Spring Data JPA 按日期和时间进行搜索?

php - Laravel 有时显示 404 未找到页面

spring - 405 不允许使用 POST 方法

json - Spring默认消费和生产

jsf - Intellij idea 新项目与 jsf 不工作