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
我需要添加scanBasePackages嗎?因爲我將端點註釋爲組件。 – kamaci
我相信你仍然需要使用scanBasePackages。我不認爲Spring會掃描每個查找'''@ Component'''''''''''','''@Service'''或任何後代的包。 – ootero