use*_*453 3 java spring tomcat hibernate gradle
我是春天和Gradle的新手.我正在尝试使用Tomcat服务器上的Gradle构建部署Spring应用程序.我能够生成一个war文件,但是我没有用于映射servlet的web.xml文件,也没有映射servlet.我有一个主类和配置类.
那么,为了在tomcat上部署我的应用程序我需要做什么?我找不到适合自己的最终文章.我浏览了这个http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html(本文主要涉及maven存储库/插件而不是Gradle部分)能够只生成war文件.由于我没有任何web.xml,我不知道如何部署它.任何人都可以帮我这个.
buildscript {
repositories {
maven { url "" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle- plugin:1.0.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'abc-service'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "" }
}
dependencies {
compile("org.springframework:spring-jdbc")
compile("org.springframework:spring-oxm")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("com.h2database:h2")
compile("mysql:mysql-connector-java:5.1.16")
compile("com.paypal.sdk:rest-api-sdk:1.4.1")
compile("commons-dbcp:commons-dbcp:1.4")
compile("com.sun.mail:javax.mail:1.5.5")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat");
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
}
Run Code Online (Sandbox Code Playgroud)
以下是我的用户控制器的示例代码:
@Controller
public class AuthenticationController {
@RequestMapping(value = "/authentication/userLogin")
public @ResponseBody
ResponseEntity<Object> user(@RequestParam(value="username", required=true) String username,
@RequestParam(value="password", required=true) String password,
HttpServletResponse response) {
return this.authenticationService.loginUser(username, password);
}
@RequestMapping(value = "/authentication/register")
public @ResponseBody
Response register(@RequestBody String jsonRequest,
HttpServletResponse response) {
AuthenticationService authenticationService = new AuthenticationService();
Response registerResponse = authenticationService.registerUser(jsonRequest);
return registerResponse;
}}
Run Code Online (Sandbox Code Playgroud)
只要您定义了控制器,您的应用程序就可以实现.Spring-boot的最佳功能之一是,它可以为您完成大部分肮脏的工作.您必须使用控件和url路径定义注释@RequestMapping("/your-path")
.另外,用类注释类@RestController
.如果你可以给这个应用程序代码的url,如果它有所有必要的定义,我将能够进一步研究它.此外,我会建议一些改变.
将spring-boot-version更新到最新的1.3.5
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
Run Code Online (Sandbox Code Playgroud)
更新Gradle版本
task wrapper(type: Wrapper) {
gradleVersion = '2.13'
}
Run Code Online (Sandbox Code Playgroud)
还要更新其他依赖项.然后调用以下命令.
gradle wrapper
./gradlew clean build
Run Code Online (Sandbox Code Playgroud)
第一行将下载将项目设置为使用Gradle版本2.13运行并将其下载到临时目录,而不管计算机中安装的Gradle版本.第二行将使用下载的最新Gradle构建您的项目.然后检查您的<project-root-directory>/build/libs
目录.你会看到abc-service-0.1.0-SNAPSHOT.war
文件.只需拿起那场战争并将其部署到您的Tomcat服务器中.
如果要将war文件部署到Tomcat,则应将应用程序构建名称添加到URL以访问映射,因为Tomcat使用war文件的文件名作为上下文路径(请单击此处).例如:localhost:8080/abc-service-0.1.0-SNAPSHOT/your-mapping/
.您可以使用baseName属性指定战争的命名方式.例如,如果您不希望它在您的应用程序URL中,则可以删除版本号.
war {
baseName = 'abc-service'
}
Run Code Online (Sandbox Code Playgroud)
或者,在复制到Tomcat之前,您可以简单地将war文件重命名为Gradle构建后所需的任何根上下文.
我喜欢spring-boot的另一个特性是,你不需要外部Tomcat服务器来部署spring-boot应用程序.它已经有一个嵌入式Tomcat服务器.要利用此功能,可以将应用程序构建为jar文件,并作为java应用程序运行.要做到这一点,首先,摆脱线apply plugin: 'war'
.并将您的war配置更改为jar配置.
jar {
baseName = 'abc-service'
version = '0.1.0'
}
Run Code Online (Sandbox Code Playgroud)
并将其作为jar文件运行.
java -jar <project-root-directory>/build/libs/abc-service-0.1.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
如果您使用的是jar文件,则只需使用您指定的映射即可.例如:localhost:8080/your-mapping
.
根据您的控制器,您应该为用户进行REST调用(身份验证)localhost:8080/authentication/userLogin?username=<user-name>&password=<pass-word>
.如果您错过了用户名或密码,您将收到错误,因为它们是required=true
基于您的代码的required()字段.此外,您的注册方法应配置为post,您应该传递请求正文.您可以使用Advanced REST Client/POSTMAN Google Chrome扩展程序发布请求.
@RequestMapping(value = "/authentication/register", method = RequestMethod.POST)
public @ResponseBody Response register(@RequestBody String jsonRequest,
HttpServletResponse response) {
AuthenticationService authenticationService = new AuthenticationService();
Response registerResponse = authenticationService.registerUser(jsonRequest);
return registerResponse;
}}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5100 次 |
最近记录: |