Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy Spring application using Gradle build on Tomcat server

I am new to spring and Gradle. I am trying to Deploy Spring application using Gradle build on Tomcat server. I am able to generate a war file but i do not have web.xml file for mapping servlets and neither I have mapping servlets. I have a main class and configuration class.

So, In order to deploy my application on tomcat what all I need to do ? I couldn't find a proper end to end article for this. I went through this http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html (This article is mostly covering maven repository/plugin not the Gradle part) and am able to generate only the war file. As I do not have any web.xml I am not sure how to deploy this. Can anyone please help me with this.

   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'
 }

Here is sample code for my user controller:

 @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;
 }}
like image 248
user5876453 Avatar asked Mar 12 '23 00:03

user5876453


1 Answers

Your application is good to go as it is, as long as you have controller defined. One of the best feature of Spring-boot is, it does most of the dirty work for you. You have to define the controller and url path as annotation using @RequestMapping("/your-path"). Also, annotate the class with @RestController. If you could give the url for this application code, I will be able to look into it further if it has all necessary definitions. Besides, I would recommend a few changes.

Update spring-boot-version to latest 1.3.5

classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")

Update Gradle Version

task wrapper(type: Wrapper) {

    gradleVersion = '2.13'
}

Update other dependencies as well. Then call the following commands.

gradle wrapper
./gradlew clean build 

The first line will download set your project to run with Gradle version 2.13 and download it to a temporary directory irrespective of the Gradle version installed in your computer. Second line will build your project using that downloaded latest Gradle. Then check your <project-root-directory>/build/libs directory. You will see abc-service-0.1.0-SNAPSHOT.war file. Just pick up that war and deploy it in you Tomcat server.

If you are deploying the war file to Tomcat, you should add the application build name to URL to access your mappings, since Tomcat use the filename of the war file for context path (check here). Eg: localhost:8080/abc-service-0.1.0-SNAPSHOT/your-mapping/. You can specify how your war should named using baseName property. For example, you can get rid of the version number if you don't want it in your application URL.

war {
   baseName = 'abc-service'
}

Or you can simply rename your war file to whatever the root context you want after Gradle build before copying into Tomcat.

Another feature I like about spring-boot is, you don't need an external Tomcat server for deploying spring-boot application. It already has an embedded Tomcat server. To take advantage of this feature, you build you application as a jar file, and run as java application. To do so, first, get rid of the line apply plugin: 'war'. And change your war configuration to jar configuration.

jar {
   baseName = 'abc-service'
   version =  '0.1.0'
}

And run this as jar file.

java -jar <project-root-directory>/build/libs/abc-service-0.1.0-SNAPSHOT.jar

If you are using jar file, you can simply use whatever your mapping you specified. Eg: localhost:8080/your-mapping.

Based on your controller your REST call for the user (authenticating) should be localhost:8080/authentication/userLogin?username=<user-name>&password=<pass-word>. If you miss either user name or password you will get the error since they are required (required=true) fields based on your code. Also your register method should be configured as post and you should pass a request body.You can use Advanced REST Client/ POSTMAN Google Chrome extension to make post request.

@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;
}}
like image 147
techtabu Avatar answered Apr 06 '23 00:04

techtabu