spring boot war deployment to tomcat with status:OK But gives 404 on access - gradle

My configration class looks as follow
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackages = "com.projectx")
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Also build.gradle file looks like this
apply plugin: 'java'
sourceCompatibility=1.8
targetCompatibility=1.8
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
mainClassName = 'com.projectx.data.config.Application'
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")
}
}
war {
baseName = 'gs-accessing-data-jpa'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/libs-release" }
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
So wherever i am deploying this project to tomcat it is showing following error. As I have mate all requirement to convert jar to war in spring not sure what is wrong this my project.
It gives 404 NOT FOUND.(I am using STS 3.6.0 with Gradle plugin)

Related

Gradle not creating a Tomcat deployable war

I'm running into an issue with building a war with Gradle 4.8.1. Here is the build.gradle:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
archivesBaseName = 'sample'
//war {
// archiveName = 'sample.war'
// group = 'my.group'
// version = '2.0.0-SNAPSHOT'
//}
sourceCompatibility = 1.8
repositories {
jcenter()
maven { url "${nexusUrl}/content/groups/public" }
}
dependencies {
// Spring
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-log4j2'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
compile 'org.springframework.boot:spring-boot-starter-web'
testCompile 'org.springframework.boot:spring-boot-starter-test'
// Lombok
compile "org.projectlombok:lombok:1.18.0"
// Data
compile ('org.postgresql:postgresql') {
exclude module: 'slf4j-simple'
}
// Http
compile "org.apache.httpcomponents:httpclient:4.5.5"
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "${nexusUrl}/content/groups/public/") {
authentication(userName: nexusUsername, password: nexusPassword)
}
snapshotRepository(url: "${nexusUrl}/content/repositories/snapshots") {
authentication(userName: nexusUsername, password: nexusPassword)
}
pom.version = '2.0.0-SNAPSHOT'
pom.artifactId = 'sample'
pom.groupId = 'my.group'
}
}
}
I also tried removing the 'maven' plugin, archivesBaseName, and the uploadArchive task while uncommenting the war task and I get the same result. When using uploadArchive the war deploys to the nexus server fine and I get no errors. When deploying the war to tomcat (in both instances) tomcat 7 and 8 throw no errors and I receive no logs from either catalina or the project, although archiveName inside the war task does not properly rename the war. I have tried this on two other machines/tomcat instances with the same result. When building this as a fat jar, or running this in IntelliJ, everything works as expected (including the logging).
Any help or direction would be greatly appreciated.
It sounds like it is not initializing the servlet at all. Make sure to extend the SpringBootServletInitializer for WAR deployment.
Spring Boot Docs - Traditional Deployment
I was also facing the same issue. The issue is Spring application doesnt get initialized when the war is deployed on tomcat.
After a lot of struggle, I figured out that I needed to extend SpringBootServletInitializer in my application. So my effective code looks like
public class SyncApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SyncApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SyncApplication.class, args);
}
}
Looks like this SpringBootServletInitializer directs war plugins generate bootstrapping code while building the war, and thus spring context is initialized while deploying the app.

Spring Boot 1.5.9 context-path HEAD response not working

I have problem with setting up HEAD resposnse from RestController in Spring Boor. I'm currently using Spring Boot version 1.5.9.
I have settup new project for demostrating this problem. In application.properties I have just one line:
server.context-path=/api/v1
My RestController "TestController.java"
#RestController
public class TestController {
#GetMapping("/test")
public String test() {
return "test";
}
}
When I try to get only header with HTTP HEAD request on url http://localhost:8080/api/v1/test, then the response stack and I don't get any response. In app console there is no errors.
If I remove server.context-path=/api/v1 from application.properties. Then HEAD request to http://localhost:8080/test is working as expected.
Thank you for any help,
Martin
Here is the build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-websocket')
// compile('com.microsoft.sqlserver:sqljdbc4')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Spring bootRun not available from within Eclipse gradle

I am just starting out with Spring and have successfully setup and am running the RESTful tutorial using gradle buildship within Eclipse.
https://spring.io/guides/gs/rest-service/#initial
However, one thing I cannot get working within Eclipse is it says you can run the application from gradle rather than the jar directly by the command:
gradle bootRun
but bootRun is not a task created by the gradle script, only 'run' (which does not work). All the other entries, like 'build', etc. are there.
It's not a big deal as I can run the jar from the command line but I'd like to run it from within Eclipse (I do not have gradle installed either, just buildship).
thanks.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.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('org.springframework.boot:spring-boot-starter-test')
}
You don't need 'gradle bootRun'
Just run that as a main method:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Missing schema module for spring-cloud-stream

Trying to use the following example from Spring Docs
#Bean
public MessageConverter userMessageConverter() throws IOException {
AvroSchemaMessageConverter avroSchemaMessageConverter {
return new AvroSchemaMessageConverter(MimeType.valueOf("avro/bytes");
}
Using Gradle as follows
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'org.springframework.boot'
dependencies {
compile('org.springframework.cloud:spring-cloud-stream')
compile('org.springframework.cloud:spring-cloud-starter-stream-kafka')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR2"
}
}
Gradle is loading the correct version of spring-cloud-streams 1.1.0.RELEASE but it does not match with the Github Repo. The artifact is missing the org.springframework.cloud.stream.schema package/source.
Am I missing something here?
The artifact org.springframework.cloud:spring-cloud-starter-stream-kafka brings in spring-cloud-stream, spring-cloud-stream-codec and related dependencies like spring-integration. You would have to explicitly define org.springframework.cloud:spring-cloud-stream-schema.
Also, you don't need to specify 'org.springframework.cloud:spring-cloud-stream' as it will be part of org.springframework.cloud:spring-cloud-starter-stream-kafka via org.springframework.cloud:spring-cloud-stream-binder-kafka.

Gradle Unable to retrieve local repository

I have the following setup in my build.gradle file
repositories {
mavenCentral()
mavenLocal()
maven {
url '/Users/me/.m2/repository'
}
A library under the directory doesn't get imported into my project. I create another project with Maven and the library is imported. What is missing in the gradle setup?
Here is the file:
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
mavenLocal()
maven {
url '/Users/vewu/.m2/repository'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'sample'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal() // <-- I need to add this one
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-stream-kafka')
compile('org.springframework.boot:spring-boot-starter-integration')
compile 'mylibrary:mypackage:1.3.18'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.RELEASE"
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
I just recognize that I need to add mavenLocal() on the repository block outside of the buildscript as well.

Resources