1

I am trying to make a simple Spring4 WebService here is a Gist with the basic code

https://gist.github.com/jrgleason/1e23b694e0facc123caa

It seems to start ok but when I access http://localhost:8080/itext I get a 404 exception. Can someone please help me with what is wrong? Is it because I am using the boot plugin?

Jackie
  • 21,969
  • 32
  • 147
  • 289

2 Answers2

1

Your application is working, check this url: http://localhost:8080/

Change you bean for http://localhost:8080/itext

@RestController
public class GreetingController {
    @RequestMapping("/itext")
    public String test(){
        System.out.println("Test");
        return "Test";
    }
}

In Spring Boot Tomcat is embedded by default, there is no need to configure tomcat.

MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • How would I control Tomcat version in this example? – Jackie Jun 18 '14 at 15:33
  • Having problems with the ctrl c using windows/cygwin still says address is in use one sec – Jackie Jun 18 '14 at 15:43
  • So this kind of works with 2 main issues... 1.) If I start a server in Cygwin (not command prompt) in win32 the Spring-Boot wrapper does not stop gracefully making things a bit of a pain. 2.) It doesn't seem to pay attention to my context root. – Jackie Jun 18 '14 at 15:50
  • As for using process manager, this doesn't work due to multiple JVMs running on my machine. I have to search using process explorer and find the right one which is a pain – Jackie Jun 18 '14 at 15:51
  • Also your build.gradle can be reduced – Jackie Jun 18 '14 at 15:51
  • Did you read my updated answer? Your code is ok! I'm using Linux, so I cant help you with windows nightmares ;) – MariuszS Jun 18 '14 at 15:52
  • Also what if I want the context root for the entire application (not just the controller) – Jackie Jun 18 '14 at 15:52
  • This app is binded to ROOT context by desing, deploy it to other context. – MariuszS Jun 18 '14 at 15:54
  • But that will not effect the context root in the war right? If that is true and you update your build.gradle to remove the unnecessary stuff I will accept – Jackie Jun 18 '14 at 15:56
  • contextRoot is generally defined outside the war – MariuszS Jun 18 '14 at 15:57
  • Dope that is right I was thinking it was in the Web.config just fix the build.gradle then – Jackie Jun 18 '14 at 15:58
0

The problem is that Spring-Boot does not seem to play nice with the tomcat-plugin (A response that can get it working will steal the star!). As noted above you can reduce to just using the spring-boot...

buildscript {
  repositories {
    mavenLocal()
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.1.RELEASE")
  }
}
apply plugin: 'war'
apply plugin: 'spring-boot'
war { baseName='itext' }
repositories {
  mavenLocal()
  mavenCentral()
}
dependencies {
  compile("org.springframework.boot:spring-boot-starter-web")
  providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
  testCompile("org.springframework.boot:spring-boot-starter-test")
}

If you do this, however, you will also need to change the Application.java to something like this....

package com.gleason.itext;
import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

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

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

    private static Class<Application> applicationClass = Application.class;
}
Jackie
  • 21,969
  • 32
  • 147
  • 289
  • My `build.gradle` is autogenerated from start.spring.io :) Btw, your answer is not related to question. – MariuszS Jun 18 '14 at 16:08
  • autogen != perfect autogen == convention, How is it not related? I would accept you build.gradle if you could convince me the other stuff is worth leaving in – Jackie Jun 18 '14 at 16:15
  • My buid.gradle is unrelated too, so it is removed now. – MariuszS Jun 18 '14 at 16:19
  • Thanks for all of your help so far, however, it seems to be failing when I deploy to standalone tomcat – Jackie Jun 18 '14 at 16:21
  • So, ask another question :) BTW. Why are you usng WAR? Use JAR, it works fine :) – MariuszS Jun 18 '14 at 16:22
  • Because it will be deployed to a tomcat server running multiple applications. Jar works great for local testing but not deployment – Jackie Jun 18 '14 at 16:23
  • No, JAR works great on production too :) Tomcat running multiple applications is not good idea anymore :) It is much easier to profile and manage separate applications. – MariuszS Jun 18 '14 at 16:25
  • Read this: http://stackoverflow.com/questions/22886083/how-do-i-run-a-spring-boot-executable-jar-in-a-production-environment – MariuszS Jun 18 '14 at 16:26