Rest 带java的spring boot+;嵌入式tomcat中的groovy_maven,控制器错误
很抱歉,如果已经回答了,我无法找到它 我已经使用SpringBoot创建了一个新项目 我的要求是我有一些java类,一些groovy类,它们应该能够相互调用 我正在使用maven并运行我的嵌入式tomcat mvn spring启动:运行 问题是,RestController是Java类,我可以称之为restURL 但是Groovy中的控制器不能被调用,并且给了我错误Rest 带java的spring boot+;嵌入式tomcat中的groovy_maven,控制器错误,rest,groovy,spring-boot,Rest,Groovy,Spring Boot,很抱歉,如果已经回答了,我无法找到它 我已经使用SpringBoot创建了一个新项目 我的要求是我有一些java类,一些groovy类,它们应该能够相互调用 我正在使用maven并运行我的嵌入式tomcat mvn spring启动:运行 问题是,RestController是Java类,我可以称之为restURL 但是Groovy中的控制器不能被调用,并且给了我错误 curl localhost:8080/ {"timestamp":1455913384508,"status":404,"er
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!";
}
}
4.0.0
org.springframework
弹簧靴
0.1.0
org.springframework.boot
spring启动程序父级
1.3.2.1发布
org.springframework.boot
SpringBootStarterWeb
org.codehaus.groovy
groovy all
2.3.7
1.8
org.springframework.boot
springbootmaven插件
maven编译器插件
3.1
groovy eclipse编译器
org.codehaus.groovy
groovy eclipse编译器
2.9.1-01
org.codehaus.groovy
groovy eclipse批处理
2.3.7-01
app.groovy:
包裹你好
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.bind.annotation.RequestMapping;
@RestController
类将实际运行{
@请求映射(“/home”)
字符串home(){
返回“你好,世界!”
}
}
Application.java
套餐你好;
导入java.util.array;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.ApplicationContext;
@SpringBoot应用程序
公共类应用程序{
公共静态void main(字符串[]args){
ApplicationContext ctx=SpringApplication.run(Application.class,args);
println(“让我们检查SpringBoot提供的bean:”;
字符串[]beanNames=ctx.getBeanDefinitionNames();
数组。排序(beanNames);
for(字符串beanName:beanName){
System.out.println(beanName);
}
}
}
控制器类
套餐你好;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.bind.annotation.RequestMapping;
@RestController
公共类Hello控制器{
@请求映射(“/hello”)
公共字符串索引(){
ThisWillActuallyRun t=新的ThisWillActuallyRun();
字符串v=t.home();
System.out.println(“来自groovy的值=“+v”);
回复“来自Spring Boot的问候!”;
}
}
这项工作:
curl localhost:8080/你好
来自春靴的问候
非常感谢你的帮助 我看您的Groovy控制器没有问题
这将实际运行
我担心的一点是,您有两个独立的控制器,但没有在每个控制器的类顶部提供@RequestMapping(path=“controllerpath”)
。您没有为控制器指定唯一的上下文(相对路径)
此外,curl命令仅指向“/”。我看不到这方面的任何映射。
如果你蜷缩到“/家”,它可能会起作用,就像你为“/你好”所做的那样。无论如何,给出控制器级路径也是一种更好的做法
如果在两个控制器的顶部注释@RequestMapping,URL的外观示例可能如下所示:
@RestController
@RequestMapping(path="destination")
class ThisWillActuallyRun {
@RequestMapping("/home")
String home() { }
}
@RestController
@RequestMapping(path="greeting")
public class HelloController {
@RequestMapping("/hello")
public String index() {}
}
然后到达两个端点的步骤如下所示:
@RestController
@RequestMapping(path="destination")
class ThisWillActuallyRun {
@RequestMapping("/home")
String home() { }
}
@RestController
@RequestMapping(path="greeting")
public class HelloController {
@RequestMapping("/hello")
public String index() {}
}
这起作用了。非常感谢。Spring启动站点按原样复制。缺少@RequestMapping(path=“destination”),我是新来的,所以不能投票,也没有出现“接受答案”:)几个月后我可能会有一些名气。