【发布时间】:2016-07-26 10:26:27
【问题描述】:
谁能告诉我如何配置@RestController?
我这样做:
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/test.htm")
@ResponseBody
String home() {
return "Hello Worlds!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
@Controller
public class MusicControler {
class Test{
String name;
int age;
}
@RequestMapping(value = "/MyController")
public Test MyController() {
Test test = new Test();
test.name = "zl.shi";
test.age = 16;
return test;
}
}
当我请求 /test.htm 时,没关系,但我收到 /testController.htm 的响应 404。有人可以帮帮我吗?
【问题讨论】:
-
您尚未为
/testController.htm定义映射,因此无法找到它。 -
可以使用@RestController 代替@Controller 注解。 @RestController 意味着您从方法返回的任何对象都将作为响应主体发送给调用它的客户端。 @RestController = @Contoller +@ResponseBody .
-
你没有到
/testController.htm的映射,怎么找到?将/MyController更改为/testController.htm。
标签: java spring spring-mvc spring-boot