sequence
This paper mainly studies dubbo DubboSwaggerService
DubboSwaggerService
Dubbo – 2.7.2 / dubbo – RPC/dubbo xml-rpc – rest/SRC/main/Java/org/apache/dubbo/RPC/protocol/rest/integration / / DubboSwaggerSe swagger rvice.java
@Path("dubbo")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
public interface DubboSwaggerService {
@GET
@Path("swagger")
public Response getListingJson(@Context Application app, @Context ServletConfig sc,
@Context HttpHeaders headers, @Context UriInfo uriInfo) throws JsonProcessingException;
}
Copy the code
- DubboSwaggerService defines the getListingJson method
DubboSwaggerApiListingResource
Dubbo – 2.7.2 / dubbo – RPC/dubbo xml-rpc – rest/SRC/main/Java/org/apache/dubbo/RPC/protocol/rest/integration / / DubboSwaggerAp swagger iListingResource.java
@Service
public class DubboSwaggerApiListingResource extends BaseApiListingResource implements DubboSwaggerService {
@Context
ServletContext context;
@Override
public Response getListingJson(Application app, ServletConfig sc,
HttpHeaders headers, UriInfo uriInfo) throws JsonProcessingException {
Response response = getListingJsonResponse(app, context, sc, headers, uriInfo);
response.getHeaders().add("Access-Control-Allow-Origin"."*");
response.getHeaders().add("Access-Control-Allow-Headers"."x-requested-with, ssi-token");
response.getHeaders().add("Access-Control-Max-Age"."3600");
response.getHeaders().add("Access-Control-Allow-Methods"."GET,POST,PUT,DELETE,OPTIONS");
returnresponse; }}Copy the code
- DubboSwaggerApiListingResource inherited the swagger – jaxrs BaseApiListingResource, at the same time, realize the DubboSwaggerService interface; Its implementation of the getListingJson method first calls the parent’s getListingJsonResponse to get the response, and then adds cross-domain Settings to the header
The instance
Dubbo – 2.7.2 / dubbo – RPC/dubbo xml-rpc – rest/SRC/test/Java/org/apache/dubbo/RPC protocol/rest/integration / / DubboSwaggerAp swagger iListingResourceTest.java
public class DubboSwaggerApiListingResourceTest {
private Application app;
private ServletConfig sc;
@Test
public void test() throws Exception { DubboSwaggerApiListingResource resource = new DubboSwaggerApiListingResource(); app = mock(Application.class); sc = mock(ServletConfig.class); Set<Class<? >> sets = new HashSet<Class<? > > (); sets.add(SwaggerService.class); when(sc.getServletContext()).thenReturn(mock(ServletContext.class)); when(app.getClasses()).thenReturn(sets); Response response = resource.getListingJson(app, sc, null, new ResteasyUriInfo(new URI("http://rest.test")));
Assertions.assertNotNull(response);
Swagger swagger = (Swagger)response.getEntity();
Assertions.assertEquals("SwaggerService",swagger.getTags().get(0).getName());
Assertions.assertEquals("/demoService/hello",swagger.getPaths().keySet().toArray()[0].toString()); }}Copy the code
- This validates the swagger path of the Entity returned by Resource. GetListingJson
summary
DubboSwaggerService defines the getListingJson method; DubboSwaggerApiListingResource inherited the swagger – jaxrs BaseApiListingResource, at the same time, realize the DubboSwaggerService interface; Its implementation of the getListingJson method first calls the parent’s getListingJsonResponse to get the response, and then adds cross-domain Settings to the header
doc
- DubboSwaggerService