2016-02-19 43 views
1

對不起,如果這已經回答,我無法找到它。java + groovy _maven在嵌入式tomcat,控制器的彈簧啓動,但錯誤

我已經使用spring引導創建了新項目。

我的要求是,我有一些java類,一些常規類,他們應該能夠互相呼叫。

我使用Maven和由 MVN春季啓動運行我的tomcat嵌入:運行 問題是,RestController這是Java類那裏,我可以把它叫做REST URL。

但是,在Groovy中的控制器不能被調用,並給我錯誤。

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

好的部分是我可以從java調用groovy類。

以下是我的文件。

 <?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!"; 
    } 

} 

這工作: 捲曲本地主機:8080 /你好,從春天啓動 問候!

非常感謝您的幫助。

回答

1

我沒有看到你的Groovy控制器ThisWillActuallyRun 一個問題我想有一個問題是,你有2個獨立的控制器,但在你的類的每個控制器上的頂部沒有提供@RequestMapping(path="controllerpath")。您沒有爲控制器指定唯一的上下文(相對路徑)。

另外,你的curl命令只會去「/」。我沒有看到任何映射。 如果你捲曲到「/ home」,就像你爲「/你好」所做的一樣。無論如何,最好給出一個控制器級別的路徑。

的,如果你註釋的@RequestMapping在你的2個控制器的頂部可能看起來像URL會怎樣看一個例子:

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

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

然後到達終點2會是什麼樣子:

http://localhost:8080/destination/home

http://localhost:8080/greeting/hello

+0

這工作。非常感謝。 Spring引導網站的原樣複製。這是缺少@RequestMapping(路徑=「目的地」) – bhai

+0

我是非常新的SO,不能投票和「接受答案」沒有出現。 :(幾個月後,我可能會有一些聲譽。 – bhai