Getting “404 - Not found” error with Wildfly and springboot

徘徊边缘 提交于 2019-12-23 16:18:21

问题


Hello i am trying to learn wildfly and springboot with a very simple application using eclipse. The project name is springboot-test. All the classes including the main method class are in the same package.

Main method class is called 'App' which has the following code:

 @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();
    }
}

Here are the server logs:

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)

Url i am accessing is: http://localhost:8080/springboot-test/index


回答1:


Since you are using wildfly for deployment , I'm hoping you are generating war file and server logs seems to support my statement

Do you have SpringBootServletInitializer class ??? if not that's the issue

You need something like this

@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();
    }
}

I' not sure is there any issue in annotating @SpringBootApplicationand @RestController in one class. But my suggestion is to keep it separate for maintenance

@RestController
public class Mycontroller{

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



回答2:


Your Server log says:

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'

It should be accessible at: http://localhost:8080/springboot-test-0.0.1-SNAPSHOT/index

Configure your Maven file to use <finalName>${project.artifactId}</finalName> if you like to register your address.



来源:https://stackoverflow.com/questions/48700147/getting-404-not-found-error-with-wildfly-and-springboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!