简体   繁体   中英

Cannot execute SpringBoot endpoint on Tomcat

I developed a following endpoint using SpringBoot:

@RestController
@RequestMapping("api/v1")
public class UserResource {

    @GetMapping("users")
    public ResponseEntity<List<User>> getUsers() {
        return ResponseEntity.ok().body(List.of(new User("George", "Walker")));
    }

}

The endpoint works when I launched it using bootRun Gradle task.

The endpoint can be reached using: http://localhost:8080/api/v1/users

在此处输入图像描述

Then I build a war file using war Gradle Task and I deploy it using Tomcat.

在此处输入图像描述

I try to reach the same endpoint using the URL: http://localhost:8080/user-service-api-0.0.1-SNAPSHOT-plain/api/v1/users , but it fails. The Tomcat is up, the app is deployed, but the endpoint is not accessible.

In addition to that I have in build.gradle entry: org.springframework.boot:spring-boot-starter-tomcat and a class:

package net.bean.userserviceapi;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}

What am I doing wrong?

Thank you.

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.traditional-deployment

The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. Doing so makes use of Spring Framework's servlet 3.0 support and lets you configure your application when it is launched by the servlet container.

first step : is to configure your maven file to generate a war package.

<packaging>war</packaging>

second step : is to make your main class extends SpringBootServletInitializer

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

Check this link for more details.

The reason is that I tried to run it on Apache Tomcat 10. It runs smoothly on Tomcat 9.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM