使用Spring启动将RESTful服务部署到AWS

时间:2016-10-28 01:04:54

标签: java rest maven amazon-web-services spring-boot

我正在尝试将一个简单的Spring Boot RESTful服务部署到AWS beanstalk。我正在使用mvn clean包来获得战争。这是我的代码:

 //@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

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

package hello;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

@RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {
    return "Hello, " + name + "!";
}
}

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
    <tomcat.version>8.0.3</tomcat.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

但是当我尝试访问(load-balancer-url)/ hello / World时,我收到404错误。对我做错的任何帮助?谢谢。

2 个答案:

答案 0 :(得分:2)

Spring Boot | Create a deployable war file

来自文档:

  

生成可部署的war文件的第一步是提供一个   SpringBootServletInitializer子类并覆盖其配置   方法。这利用了Spring Framework的Servlet 3.0支持和   允许您在应用程序启动时配置您的应用程序   servlet容器。通常,您更新应用程序的主类   扩展SpringBootServletInitializer:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

答案 1 :(得分:0)

您也可以使用ElasticBeanstalk安装它。 首先,您需要战争文件。转到应用程序文件夹,然后按照以下步骤使用Maven进行打包。

mvn package -DskipTests

(在Windows计算机中,复制mvnw.cmd并将副本命名为mvnw.bat)

完成打包任务后,您将在“目标”文件夹中找到您的projectname-snapshot.war。

将war文件上传到Beanstalk之后,点击配置更多选项 并转到软件部分,只需将SERVER_PORT设置为5000,如下所示。设置后,点击创建环境

enter image description here