【发布时间】:2018-11-27 08:46:36
【问题描述】:
我正在构建我的第一个 Spring Boot 应用程序。但我无法让我的 requestMapping 控制器正确回答。
这是我的主要课程:
package com.hello.world;
@SpringBootApplication
public class HelloWorld implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class, args);
}
@Override
public void run(String... args) throws Exception {
....
}
}
这是我的 RestController:
package com.hello.world.controllers;
@RestController
public class UrlMappingControllers {
@RequestMapping("/hi")
String home() {
return "Hello World!";
}
}
如果我查看日志,我可以看到“/hi”映射:
restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hi]}" onto java.lang.String com.hello.world.controllers.UrlMappingControllers.home()
但是当我访问:http:localhost:8080/hi 我得到一个空白页面,我希望看到“Hello World”文本。
为什么我得到一个空白页?
--- 编辑 ----
我刚刚意识到只有在添加 cxf 服务时才会得到空白页。我认为是因为这个类上的@configuration注解:
package com.hello.world.helloWorld.configuration;
@Configuration
public class CXFConfiguration {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
@Bean(name=Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus springBus = new SpringBus();
return springBus;
}
@Bean
public Endpoint endpointGreentingService() {
EndpointImpl endpoint = new EndpointImpl(springBus(), new GreetingServiceImpl());
endpoint.getFeatures().add(new LoggingFeature());
endpoint.publish("/GreetingService");
return endpoint;
}
}
可能有关系吗?
【问题讨论】:
-
将
@ResponseBody注释添加到您的home方法中。 -
@ResponseBody注释在这里不会产生影响,该类使用@RestController进行注释,从而使@ResponseBody过时 -
你试过公开
home()吗? -
尝试在您的
@RequestMapping中添加method = RequestMethod.GET,例如@RequestMapping(value = "/hi", method = RequestMethod.GET)
标签: spring spring-mvc spring-boot