0
votes

So I'm trying to start a new web project in IntelliJ using the Spring option in the project configuration setups, but when I finally get the project setup, it seems as though any new controllers I make are ignored even though I annotate them. The @RequestMapping seems to never get mapped. All that I have ever been able to access is files located in the root of the web directory.

After never being able to get this to work I switched to Gradle so that I could import some additional libraries to test my project. My gradle file looks like the following:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
    }
    test {
        java {
            srcDirs = ['src/main/test']
        }
    }
}

jar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
    testCompile "org.mockito:mockito-core:1.+"
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

Is there some sort of config file I have to fill out to tell the project where to look for controllers or is the pixie dust not working like I expect it to?

Here's what my controller looks like:

@RestController
public class GreetingController {
    public static final String greeting = "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(greeting, name));
    }
}

And the class annotated with @SpringBootApplication:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

And my folder structure:

Test
- .gradle
- .idea
- build
- gradle
- out
- src
  - main
    - java
      - controllers
        - Application.java
        - GreetingController.java
    - test
- web
  - WEB-INF
    - (empty)

I'm really confused about how Spring works at this point because the annotations haven't come into play yet. Am I missing a config file? The Spring starter tutorial says I don't need one, but maybe it's because I'm using IntelliJ.

One last thing to note is that I'm running using a local Tomcat instance.

Thanks all!

1

1 Answers

1
votes

If you deploy your application in a Tomcat, then you have to create a class which extends SpringBootServletInitializer to boot up the Spring container. Your Application class with the main method is only called when you run the JAR/WAR with java -jar yourfile.jar with the embedded Tomcat (which is included by default with Spring Boot Starter Web).

See the documentation: http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#howto-create-a-deployable-war-file