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.
What am I doing wrong?
Thank you.
CodePudding user response:
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.
CodePudding user response:
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.