bhai
bhai

Reputation: 231

spring boot with java + groovy _maven in embedded tomcat, controller but error

Sorry if this is already answered, I am not able to find it.

I have created new project using spring boot.

My requirements are that I have some java classes, some groovy classes and they should be able to call each others.

I am using maven and running my embedded tomcat by mvn spring-boot:run Problem is, RestController which is Java Class is there and I am able to call it REST URL.

But the controller which is in Groovy, is not able to be called and gives me error.

curl localhost:8080/
{"timestamp":1455913384508,"status":404,"error":"Not Found","message":"No message available","path":"/"}

Good part is that I am able to call groovy class from java.

Below are my files.

        <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>org.springframework</groupId>
        <artifactId>gs-spring-boot</artifactId>
        <version>0.1.0</version>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.2.RELEASE</version>
        </parent>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>2.3.7</version>
            </dependency>
        </dependencies>

        <properties>
            <java.version>1.8</java.version>
        </properties>


        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher -->
                    <version>3.1</version>
                    <configuration>
                        <compilerId>groovy-eclipse-compiler</compilerId>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-eclipse-compiler</artifactId>
                            <version>2.9.1-01</version>
                        </dependency>
                        <!-- for 2.8.0-01 and later you must have an explicit dependency on 
                            groovy-eclipse-batch -->
                        <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-eclipse-batch</artifactId>
                            <version>2.3.7-01</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>

    </project>


    app.groovy:
package hello

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;


@RestController
class ThisWillActuallyRun {

    @RequestMapping("/home")
    String home() {
        return "Hello World!"
    }

}


    Application.java
package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}




    Controller class
package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index() {

        ThisWillActuallyRun t = new ThisWillActuallyRun() ; 
        String v = t.home() ;
        System.out.println("value from groovy="+v) ; 
        return "Greetings from Spring Boot!";
    }

}

This works: curl localhost:8080/hello Greetings from Spring Boot!

Thanks a lot for the help.

Upvotes: 1

Views: 2260

Answers (1)

pczeus
pczeus

Reputation: 7868

I don't see a problem with your Groovy controller ThisWillActuallyRun One concern I would have is that you have 2 separate controllers, but did not provide a @RequestMapping(path="controllerpath") at the top of your class on each controller. You did not specify a unique context (relative path) to your controller.

In addition, your curl command only goes to "/". I don't see any mapping for that. It may work if you curl to "/home", just like you did for "/hello". Regardless, it is a better practice to give a controller level path as well.

An example of how the URL would look if you annotated the @RequestMapping at the top of your 2 controllers might look like:

@RestController
@RequestMapping(path="destination")
class ThisWillActuallyRun {
    @RequestMapping("/home")
    String home() { }
}

@RestController
@RequestMapping(path="greeting")
public class HelloController {
    @RequestMapping("/hello")
    public String index() {}
}

Then to reach the 2 endpoints would look like:

http://localhost:8080/destination/home

http://localhost:8080/greeting/hello

Upvotes: 1

Related Questions