将 CodeStar Spring MVC 项目改为 Spring Boot [英] Change CodeStar Spring MVC project to Spring Boot

查看:47
本文介绍了将 CodeStar Spring MVC 项目改为 Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 IDE 中运行时完美运行的 Spring Boot 项目.我想通过 AWS CodeStar 运行它.不幸的是,CodeStar 创建的默认 Spring 模板使用的是 Spring MVC.

我不能只用我的 Spring Boot 项目覆盖默认的 Spring MVC 项目(它不起作用).我可以将我的一些资源复制到 MVC 项目中,例如 index.html 并且有效.但是,像 Thymeleaf 这样的功能就不起作用了.由于这个原因和其他原因,我想将提供的 Spring MVC 更改为我已经拥有的 Spring Boot 结构.

我按照此处的说明操作:

解决方案

我了解到您表示之前您尝试使用您的 Spring Boot 项目并进行了一些修改但没有成功,但我认为它实际上有可能成功部署您在 AWS CodeStar 上的申请,这将是我的建议.

我还意识到在您的屏幕截图中包含了几个必需的工件和类,但请仔细检查您在将应用程序部署到 AWS CodeStar 时是否遵循了这些步骤.

让我们从在本地运行的 Spring Boot 项目的原始版本开始,不做任何修改,然后执行以下更改.

首先,如您分享的 GitHub 链接 所示,请确保您在您的项目中包含以下文件.AWS 的部署基础设施需要它们:

  • appspec.yml
  • buildspec.yml
  • template.yml
  • template-configuration.json
  • 整个scripts目录

请根据您的特定需求调整任何必要的配置,尤其是 template-configuration.json.

然后,在您的 pom.xml 中执行以下修改.Spring Boot 需要其中一些作为 传统部署 和其他是 AWS CodeStar 中部署所必需的.

确保将 packaging 表示为 war:

<packaging>war</packaging>

为确保嵌入的 servlet 容器不会干扰部署 war 文件的 Tomcat,请按照上述文档中的建议将 Tomcat 依赖项标记为已提供:

<!-- ... --><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖><!-- ... --></依赖项>

或者在你的pom.xml中排除Tomcat依赖:

<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><排除事项><排除><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></排除></排除项></依赖>

如有必要,请使用某种配置文件应用此排除,该配置文件允许您同时在本地和外部 servlet 容器中启动 Spring Boot.

接下来对maven war插件进行参数化以符合AWS CodeStar部署需求:

<插件管理><插件><!-- ... --><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.2.2</version><配置><warSourceDirectory>src/main/webapp</warSourceDirectory><warName>ROOT</warName><failOnMissingWebXml>false</failOnMissingWebXml></配置></插件><!-- ... --><插件></pluginManagement></build>

我认为没有必要,但为了避免任何类型的问题,请调整最终build的名称:

ROOT

最后,正如 Spring 文档,确保你的 MyProjectApplication - 我假设这个类是你的主要入口点子类 SpringBootServletInitializer 并相应地覆盖 configure,例如:

@SpringBootApplication公共类 MyProjectApplication 扩展 SpringBootServletInitializer {@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(MyProjectApplication.class);}公共静态无效主(字符串 [] args){SpringApplication.run(MyProjectApplication.class, args);}}

请随时根据您的特定用例调整课程.

通过此设置,尝试部署您的应用程序并查看它是否有效:也许您会发现某种库依赖问题,但我认为在大多数情况下它应该可以正常工作.

首先,您可以尝试按照您在项目模板中提供的说明在本地部署您稍后将部署到 AWS CodeStar 的应用程序版本,基本上,一旦配置了答案中描述的必要更改,通过运行:

mvn clean package

并在本地 tomcat 环境中部署生成的战争.请注意,ROOT 应用程序可能已经存在于标准的 tomcat 安装中(您可以通过检查 webapps 文件夹来验证它):您可以覆盖该 war 文件.

对于本地测试,您甚至可以选择不同的应用程序名称(在您的 pom.xml 文件中配置 build.finalNamewarName): 重要的是验证应用程序是否在本地运行成功.

如果您愿意,您可以选择将应用程序直接部署到 AWS CodeStar 并在必要时稍后检查日志.

无论如何,请注意两件事:一方面,如果您在应用程序中配置了任何绝对路径,这可能是您在应用程序中提到的 404 问题的原因评论.请注意,您的应用程序将使用上下文根/"部署在 Tomcat 中.

另一方面,请查看您如何配置数据库访问权限.可能您使用了 application.properties 并且很好,但是请注意,在使用应用程序时,数据库必须是可访问的:也许 Spring 无法创建必要的数据源,以及持久性管理器或相关的与相关的东西,因此,应用程序没有启动.同样,这可能是 404 错误代码的原因.

为了简化数据库连接,为了测试,乍一看,我建议您使用简单的属性来配置数据源,即驱动程序类、连接字符串、用户名和密码.如果该设置工作正常,您可以稍后启用 JNDI 或认为必要的内容.

请记住,如果您需要在 Tomcat 中更改上下文名称和/或定义数据源池,您可以在 META-INF 目录下放置一个 context.xml 文件在您的网络应用根路径中.

这个 context.xml 应该类似于:

<上下文路径=/"><资源名称=jdbc/myDS"type="javax.sql.DataSource";maxActive=100"maxIdle=30"maxWait =10000";url="jdbc:mysql://localhost:3306/myds";driverClassName="com.mysql.jdbc.Driver";用户名 =root"密码=秘密"/>

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:

解决方案

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>

这篇关于将 CodeStar Spring MVC 项目改为 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆