jax-rs示例

 

compile 'org.glassfish.jersey.core:jersey-client:2.9.1'

 

@Path("/{appId}/{env}/downloadSDK")
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
Response downloadSDK(@PathParam("appId") String appId, @PathParam("env") Integer env, @DefaultValue("javascript") String language);

 

@Override
public Response downloadSDK(String appId, Integer env, String language) {
    //swagger json url 待处理
    return generateSDK.generateSDK("http://petstore.swagger.io/v2/swagger.json", language);
}

 

public Response generateSDK(String swaggerjsonUrl, String language) {
    GeneratorInput generatorInput = new GeneratorInput();
    generatorInput.setSwaggerUrl(swaggerjsonUrl);
    try {
        String fileName = Generator.generateClient(language, generatorInput);
        if (StringUtils.isNotEmpty(fileName)) {
            File file = new java.io.File(fileName);
            byte[] bytes = org.apache.commons.io.FileUtils.readFileToByteArray(file);
            try {
                FileUtils.deleteDirectory(file.getParentFile());
            } catch (Exception e) {
                System.out.println("failed to delete file " + file.getAbsolutePath());
            }
            return Response
                    .ok(bytes, "application/zip")
                    .header("Content-Disposition",
                            "attachment; filename=\"" + language + "-client-generated.zip\"")
                    .header("Accept-Range", "bytes").header("Content-Length", bytes.length).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

 

posted @ 2017-09-20 17:43  hjzqyx  阅读(665)  评论(0编辑  收藏  举报