2016-10-03 20 views
2

我有一個作爲MVC的Spring Boot應用程序。我想在我的應用程序中使用JAX-RS,而不使用Spring註釋。我將在不同的類中使用JAX-RS註釋的組件和MVC組件。當我加入新澤西資源配置(沒有註冊任何端點):Spring Boot中的JAX-RS和MVC

@Component 
public class JerseyConfig extends ResourceConfig { 

} 

我啓動應用程序和登錄頁面不顯示。當我打開登錄頁面時,它就像文檔一樣被下載。我該如何解決它?

回答

1

1)確保您的應用的春天啓動配置文件使Spring MVC的區別,對於實例和澤西執行端點資源端點:

application.yml

... 
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...) 
server.servlet-path:/
# Jersey dispatcher servlet 
spring.jersey.application-path: /api 
... 

2) 確保了位於特定的包()通過組件即com.asimio.jerseyexample.config你的Spring應用程序啓動掃描:

@SpringBootApplication(
    scanBasePackages = { 
     "com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest" 
    } 
) 

3)新澤西配置類的實現:

package com.asimio.jerseyexample.config; 
... 
@Component 
public class JerseyConfig extends ResourceConfig { 

    ...   
    public JerseyConfig() { 
     // Register endpoints, providers, ... 
     this.registerEndpoints(); 
    } 

    private void registerEndpoints() { 
     this.register(HelloResource.class); 
     // Access through /<Jersey's servlet path>/application.wadl 
     this.register(WadlResource.class); 
    } 
} 
用JAX-RS(

4)Resource實現澤西):

package com.asimio.jerseyexample.rest.v1; 
... 
@Component 
@Path("/") 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON) 
public class HelloResource { 

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class); 

    @GET 
    @Path("v1/hello/{name}") 
    public Response getHelloVersionInUrl(@ApiParam @PathParam("name") String name) { 
     LOGGER.info("getHelloVersionInUrl() v1"); 
     return this.getHello(name, "Version 1 - passed in URL"); 
    } 
... 
} 

的更詳細如何-至c應該在幾個月前創建的博客中找到,Microservices using Spring Boot, Jersey Swagger and Docker

+0

我需要添加scanBasePackages嗎?因爲我將端點註釋爲組件。 – kamaci

+0

我相信你仍然需要使用scanBasePackages。我不認爲Spring會掃描每個查找'''@ Component'''''''''''','''@Service'''或任何後代的包。 – ootero

0

也許你可以添加路徑映射到不同的REST資源,默認情況下 「jerseyServlet」映射到[/ *],更改到/ myrest

@Configuration 
@ApplicationPath("/myrest") 
public class JerseyConfig extends ResourceConfig {}