如何将简单的Spring Boot(带有Gradle构建系统)部署到Apache Tomcat(真实服务器,而不是嵌入式服务器)? [英] How to deploy a simple Spring Boot (with Gradle build system) to Apache Tomcat (real server, not embed server)?
问题描述
我遵循本教程: http://spring.io/guides/gs/rest -service/
这是我的项目结构:
build.gradle
I follow this tutorial: http://spring.io/guides/gs/rest-service/
This is my project structure:
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
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
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
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
命令(在Apache Tomcat嵌入式服务器上运行)运行Spring Boot应用程序.但是我不知道如何在真正的Apache Tomcat上的Spring Boot应用程序之上运行(请在这一点上帮助我!)
I know how to run Spring Boot application by gradle bootRun
command (run on Apache Tomcat embed server). But I don't know how to run above Spring Boot application on real Apache Tomcat (Please help me at this point!)
推荐答案
然后我将其导入Java EE的Eclipse(带有Spring Tools Suite和Gradle插件),这是项目文件夹结构:
Then I import to Eclipse for Java EE (with Spring Tools Suite, Gradle plugin), this is project folder structure:
Greeting.java
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;
}
}
GreetingController
GreetingController
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
I modified file 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
I don't change file 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应用程序:
I can run web app in real Tomcat server:
然后我使用浏览器或Postman查看结果:
Then I use browser or Postman to view result:
http://localhost:8080/gs-rest-service/greeting?name=Vy
这篇关于如何将简单的Spring Boot(带有Gradle构建系统)部署到Apache Tomcat(真实服务器,而不是嵌入式服务器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!