@RestController返回空白

时间:2018-06-18 13:25:40

标签: spring spring-mvc spring-boot

我正在构建我的第一个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;
        }
}

它可能有关系吗?

1 个答案:

答案 0 :(得分:0)

@RestController = @Controller + @ResponseBody这意味着当你在http:localhost:8080/hi调用你的api时,响应的主体将包含home()处理程序的结果,即i-e“Hello world”。

场景背后的

@RestController使得Spring MVC使用Json Message Converter(默认情况下),并且使用@RestController注释的类中的所有处理程序方法都将返回JSON,这就是为什么你看不到你的浏览器上的文字。

您可以使用Postman或ARC来测试您的应用。某些Web浏览器(如Firefox)直接显示JSON。

相关问题