Search code examples
javaspring-bootspring-mvcthymeleafaws-codestar

Change CodeStar Spring MVC project to Spring Boot


I have a Spring Boot project that works perfectly when run in IDE. I would like to run this via AWS CodeStar. Unfortunately, the default Spring template created by CodeStar uses Spring MVC.

I cannot just overwrite the default Spring MVC project with my Spring Boot project (it doesn't work). I can copy some of my resources to the MVC project, for example index.html and that works. But then features like Thymeleaf don't work. For this and other reasons, I would like to change the provided Spring MVC into the Spring Boot structure I already have.

I followed the instructions here: https://www.baeldung.com/spring-boot-migration

Unfortunately, this doesn't help. I can create Application Entry Point and add Spring Boot dependencies without the app breaking. But when I remove the default dependencies or the configuration associated with the MVC, the app breaks. When trying to reach the URL, I get a 404 error with description:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Debugging this error message (e.g. https://www.codejava.net/java-ee/servlet/solved-tomcat-error-http-status-404-not-found) didn't help.

The message seems like it's connected to the web resource. I have my web resources in folder resources as well as webapp/resources. And Spring Boot doesn't need any location configuration, right? It uses this location by default.

Can somebody tell me what things to remove and what to add to be able to use my existing Spring Boot project?

EDIT:

This is a link to a default template for AWS CodeStar Spring web application: https://github.com/JanHorcicka/AWS-codestar-template

And this is my Spring Boot project structure:

Structure


Solution

  • I realize that you indicated that previously you tried to use your Spring Boot project with some modifications without success, but I think it could be actually a possibility to successfully deploy your application on AWS CodeStar, and it will be my advice.

    I also realized that in your screenshot you included several of the required artifacts and classes, but please, double check that you followed these steps when you deployed your application to AWS CodeStar.

    Let's start with a pristine version of your Spring Boot project running locally, without any modification, and then, perform the following changes.

    First, as indicated in the GitHub link you shared, be sure that you include the following files in your project. They are required for the deployment infrastructure of AWS:

    • appspec.yml
    • buildspec.yml
    • template.yml
    • template-configuration.json
    • The whole scripts directory

    Please, adapt any necessary configuration to your specific needs, especially, template-configuration.json.

    Then, perform the following modifications in your pom.xml. Some of them are required for Spring Boot to work as a traditional deployment and others are required by the deployment in AWS CodeStar.

    Be sure that you indicate packaging as war:

    <packaging>war</packaging>
    

    To ensure that the embedded servlet container does not interfere with the Tomcat to which the war file is deployed, either mark the Tomcat dependency as being provided as suggested in the above-mentioned documentation:

    <dependencies>
        <!-- … -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- … -->
    </dependencies>
    

    Or exclude the Tomcat dependency in your pom.xml:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    

    If necessary, apply this exclusion using some kind of profile that allows you to boot Spring Boot locally and in an external servlet container at the same time.

    Next, parameterize the maven war plugin to conform to the AWS CodeStar deployment needs:

    <build>
        <pluginManagement>
            <plugins>
              <!-- ... -->
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-war-plugin</artifactId>
                  <version>3.2.2</version>
                  <configuration>
                      <warSourceDirectory>src/main/webapp</warSourceDirectory>
                      <warName>ROOT</warName>
                      <failOnMissingWebXml>false</failOnMissingWebXml>
                  </configuration>
              </plugin>
              <!-- ... -->
           <plugins>
        </pluginManagement>
    </build>
    

    I do not consider it necessary, but just to avoid any kind of problem, adjust the name of your final build:

    <finalName>ROOT</finalName>
    

    Lastly, as also indicated in the Spring documentation, be sure that your MyProjectApplication - I assume this class is your main entry point subclass SpringBootServletInitializer and override the configure accordingly, something like:

    @SpringBootApplication
    public class MyProjectApplication extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(MyProjectApplication.class);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(MyProjectApplication.class, args);
        }
    
    }
    

    Please, feel free to adapt the class to your specific use case.

    With this setup, try to deploy your application and see if it works: perhaps you can find some kind of library dependencies problem, but I think for the most part it should work fine.

    At a first step, you can try to deploy locally the version of the application you will later deploy to AWS CodeStar following the instructions you provided in your project template, basically, once configured with the necessary changes described in the answer, by running:

    mvn clean package
    

    And deploying the generated war on your local tomcat environment. Please, be aware that probably the ROOT application already exists in a standard tomcat installation (you can verify it by inspecting the webapps folder): you can override that war file.

    For local testing you can even choose a different application name (configuring build.finalName and the warName in your pom.xml file): the important thing is verify if locally the application runs successfully.

    If you prefer to, you can choose to deploy the app directly to AWS CodeStar and inspect the logs later it necessary.

    In any case, please, pay attention on two things: on one hand, if you have any absolute path configured in your application, it can be the cause of the 404 issue you mention in the comments. Be aware that your application will be deployed in Tomcat with context root '/'.

    On the other hand, review how you configured your database access. Probably you used application.properties and it is fine, but please, be aware that when employing the application the database must be reachable: perhaps Spring is unable to create the necessary datasources, and the persistence manager or related stuff associated with and, as a consequence, the application is not starting. Again, it may be the reason of the 404 error code.

    To simplify database connectivity, for testing, at first glance, I recommend you to use simple properties for configuring your datasource, namely the driver class, connection string, username and password. If that setup works properly, you can later enable JNDI or what deemed necessary.

    Remember that if you need to change your context name and/or define a datasource pool in Tomcat you can place a context.xml file under a META-INF directory in your web app root path.

    This context.xml should look like something similar to:

    <?xml version="1.0" encoding="UTF-8"?> 
    <Context path="/"> 
    
      <Resource name="jdbc/myDS" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000" 
        url="jdbc:mysql://localhost:3306/myds" 
        driverClassName="com.mysql.jdbc.Driver" 
        username="root" 
        password="secret" 
      /> 
    
    </Context>