CodeGen for Spark Java

1,014 views
Skip to first unread message

Forrest Gump

unread,
Feb 18, 2016, 2:11:33 PM2/18/16
to Swagger
Using Spark Java, it is super easy to generate REST web services without having the need for annotations needed by JAX-RS.


Does Swagger CodeGen have a hookpoint to Spark Java? If I have a simple REST Service built with Spark Java, how can I expose it via Swagger?

Ron Ratovsky

unread,
Feb 18, 2016, 2:24:44 PM2/18/16
to swagger-sw...@googlegroups.com
As far as I know, there’s no Swagger integration for Spark at the moment.
You can create a template for the codegen to generate a Spark project though, should be relatively easy - https://github.com/swagger-api/swagger-codegen#making-your-own-codegen-modules


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Forrest Gump

unread,
Feb 18, 2016, 2:34:29 PM2/18/16
to Swagger
Thanks, let me take a look.

Forrest Gump

unread,
Feb 18, 2016, 4:35:10 PM2/18/16
to Swagger
With Spark Java if I want a GET request, it's just a matter of adding a call like this, there's no separate client or server implementation needed.

...
public static void main(String[] args) throws FISException {
   port(9091);
   Spark.staticFileLocation("/public");

   get("/product/id/:id", (rq, rs) -> new ProductList().getInstance().getProductsById(rq.params(":id")));

ProductList.getProductsById() can implement however you want to fetch the list of products, then convert to pretty JSON using this.

Gson gson = new GsonBuilder().setPrettyPrinting().create();
productJson
= "<pre>" + gson.toJson(productList) + "</pre>";

No annotation hell as in JAX-RS. Why hasn't someone looked into this already? :D

Corresponding JAX-RS implementation looks like this:

@Path("/product")

@io.swagger.annotations.Api(description = "the product API")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-02-18T19:04:14.810Z")
public class ProductApi  {
   
private final ProductApiService delegate = ProductApiServiceFactory.getProductApi();

   
@GET
   
@Path("/{id}")
   
   
@io.swagger.annotations.ApiOperation(value = "", notes = "", response = void.class, tags={  })
   
@io.swagger.annotations.ApiResponses(value = {
       
@io.swagger.annotations.ApiResponse(code = 200, message = "Get product details", response = void.class) })

   
public Response productIdGet(
@ApiParam(value = "ID",required=true) @PathParam("id") String id,@Context SecurityContext securityContext)
   
throws NotFoundException {
       
return delegate.productIdGet(id,securityContext);
   
}
}

What am I missing?

Ron Ratovsky

unread,
Feb 18, 2016, 4:37:04 PM2/18/16
to swagger-sw...@googlegroups.com
Not really sure what you’re asking.

Date: Thursday, 18 February 2016 at 13:35
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: CodeGen for Spark Java
With Spark Java if I want a GET request, it's just a matter of adding a call like this, there's no separate client or server implementation needed.

...
public static void main(String[] args) throws FISException {
   port(9091);
   Spark.staticFileLocation("/public");

   get("/product/id/:id",(rq, rs)->newProductList().getInstance().getProductsById(rq.params(":id")));

ProductList.getProductsById() can implement however you want to fetch the list of products, then convert to pretty JSON using this.

Gson gson =newGsonBuilder().setPrettyPrinting().create();
productJson
="<pre>"+ gson.toJson(productList)+"</pre>";

No annotation hell as in JAX-RS. Why hasn't someone looked into this already? :D

Corresponding JAX-RS implementation looks like this:

@Path("/product")

@io.swagger.annotations.Api(description ="the product API")
@javax.annotation.Generated(value ="class io.swagger.codegen.languages.JavaJerseyServerCodegen", date ="2016-02-18T19:04:14.810Z")
publicclassProductApi  {
   
privatefinalProductApiServicedelegate=ProductApiServiceFactory.getProductApi();

   
@GET
   
@Path("/{id}")

   
   
@io.swagger.annotations.ApiOperation(value ="", notes ="", response =void.class, tags={  })
   
@io.swagger.annotations.ApiResponses(value ={
       
@io.swagger.annotations.ApiResponse(code =200, message ="Get product details", response =void.class)})


   
publicResponse productIdGet(
@ApiParam(value ="ID",required=true)@PathParam("id")String id,@ContextSecurityContext securityContext)
   
throwsNotFoundException{
       
returndelegate.productIdGet(id,securityContext);
   
}
}

What am I missing?

--

Forrest Gump

unread,
Feb 18, 2016, 4:56:22 PM2/18/16
to Swagger
Guess two questions:

1) As a tech stack when Spark Java is so much simpler to use, why are we using JAX-RS to generate REST Services.
2) Why the Swagger team hasn't created a hook point for Spark Java? 

:) 

tony tam

unread,
Feb 18, 2016, 5:03:12 PM2/18/16
to Swagger
We can't support every framework.  If you want simple and clean you can use the Scalatra framework that is actually even simpler than Spark.

But the mechanism is there, I suggest making templates and contributing them back to the project for the world to realize how much better Spark is :)

Ron Ratovsky

unread,
Feb 18, 2016, 5:04:53 PM2/18/16
to swagger-sw...@googlegroups.com
  1. Why do people choose to use Java when Javascript is simpler? And no, I’m not saying it’s simpler, just explaining there are choices. JAX-RS is still significantly more popular than Spark. And we easily give the option to create other templates, and love such donations to the project.
  2. Time. Number of users. Resources. Your pick. Also, since in Spark it’s all in code and not annotation, there’s really no way to parse the information in runtime unless Spark itself exposes it somehow – that requires investigation as well.
For what it’s worth, we do have the swagger-inflector project which doesn’t really use JAX-RS (though it does use Jersey), and to me it’s even more elegant than Spark. So like everything else, it’s a matter of opinion. 

From: <swagger-sw...@googlegroups.com> on behalf of Forrest Gump <fgum...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Thursday, 18 February 2016 at 13:56
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: CodeGen for Spark Java

--
Reply all
Reply to author
Forward
0 new messages