java - 如何将一个简单的 Spring Boot(带有 Gradle 构建系统)部署到 Apache Tomcat(真实服务器,而非嵌入服务器)?

标签 java spring spring-mvc gradle spring-boot

我遵循本教程:http://spring.io/guides/gs/rest-service/
这是我的项目结构: enter image description here

build.gradle

buildscript{
    repositories{
        mavenCentral()
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar{
    baseName = 'gs-rest-service'
    version = '0.1.0'
}

repositories{
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies{
    compile("org.springframework.boot:spring-boot-starter-web") 
    testCompile("junit:junit")
}
task wrapper(type: Wrapper){
    gradleVersion = '2.3'
}



Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}



Greeting.java

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        super();
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

}



GreetingControler.java

package hello;

import java.util.concurrent.atomic.AtomicLong;

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));
    }

}



我知道如何通过 gradle bootRun 命令运行 Spring Boot 应用程序(在 Apache Tomcat 嵌入式服务器上运行)。但是我不知道如何在真正的 Apache Tomcat 上运行 Spring Boot 应用程序(此时请帮助我!)

enter image description here

最佳答案

关注comments's manish ,我从 http://start.spring.io/ 创建源代码库

enter image description here

然后我导入到 Eclipse for Java EE(使用 Spring Tools Suite,Gradle 插件),这是项目文件夹结构:

enter image description here

Greeting.java

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

问候 Controller

package hello;

import java.util.concurrent.atomic.AtomicLong;
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));
    }
}

我修改了文件 GsRestServiceApplication.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GsRestServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GreetingController.class, args); // <-- modify this line.
    }
}

我不更改文件 GsRestServiceApplication.java

package hello;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}

我可以在真正的 Tomcat 服务器中运行 Web 应用程序: enter image description here

然后我用浏览器或者Postman查看结果:

http://localhost:8080/gs-rest-service/greeting?name=Vy

enter image description here

关于java - 如何将一个简单的 Spring Boot(带有 Gradle 构建系统)部署到 Apache Tomcat(真实服务器,而非嵌入服务器)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35702975/

相关文章:

Java Swing : How to change GUI dynamically

Java一致同步

java - 是否可以在 spring webflow 中使用处于 <on-entry> 状态的 if 标签

spring - viewresolver 无法在 spring 中重定向到正确的 View

java - Spring中请求映射没有选择正确的 Controller 方法

java - IzPack 快捷方式噩梦

java - 无法在maven+cucumber中运行测试

java - 我如何使用 arrayAdapter 的 Intent 进入第三个 Activity ?

java - Hibernate saveOrUpdate 方法创建新条目/行而不是更新现有条目/行

java - 参数值 [vbs@gmail.com] 与预期类型 [java.lang.Long (n/a)] 不匹配;嵌套异常是 java.lang.IllegalArgumentException :