mulesoft-labs / raml-for-jax-rs

This project is all about two way transformation of JAX-RS-annotated Java code to RAML API description and back.
Other
295 stars 181 forks source link

Can you provide an example of integration with spring boot? #379

Open Shadowell opened 5 years ago

Shadowell commented 5 years ago

My raml file looks like this:

%RAML 1.0

title: Book API baseUri: http://api.datiip.com/ version: v1 types: User: type: object properties: name: type: string telephone: type: number age: type: number minimum: 0 maximum: 150 ResponseEntity: type: object properties: code: number message: string

/auser: get: responses: 200: body: application/json: type: User 500: body: application/json: type: ResponseEntity post: responses: 200: body: application/json: type: ResponseEntity

This is the endpoint interface file that I got: @Path("/auser") public interface Auser { /* / @GET @Produces("application/json") GetAuserResponse getAuser();

/* / @POST @Produces("application/json") PostAuserResponse postAuser();

class GetAuserResponse extends ResponseDelegate { private GetAuserResponse(Response response, Object entity) { super(response, entity); }

private GetAuserResponse(Response response) {
  super(response);
}

public static GetAuserResponse respond200WithApplicationJson(User entity) {
  Response.ResponseBuilder responseBuilder = Response.status(200).header("Content-Type", "application/json");
  responseBuilder.entity(entity);
  return new GetAuserResponse(responseBuilder.build(), entity);
}

public static GetAuserResponse respond500WithApplicationJson(ResponseEntity entity) {
  Response.ResponseBuilder responseBuilder = Response.status(500).header("Content-Type", "application/json");
  responseBuilder.entity(entity);
  return new GetAuserResponse(responseBuilder.build(), entity);
}

}

class PostAuserResponse extends ResponseDelegate { private PostAuserResponse(Response response, Object entity) { super(response, entity); }

private PostAuserResponse(Response response) {
  super(response);
}

public static PostAuserResponse respond200WithApplicationJson(ResponseEntity entity) {
  Response.ResponseBuilder responseBuilder = Response.status(200).header("Content-Type", "application/json");
  responseBuilder.entity(entity);
  return new PostAuserResponse(responseBuilder.build(), entity);
}

} }

How can I inherit such interfaces to write my own implementation logic flexibly, please give me a demo with spring boot integrated.

Thank you very much!

jpbelang commented 5 years ago

I don't really "integrate" with SpringBoot. I've successfully used SB in projects. I have an example project doing just that at https://github.com/jpbelang/BeerTrader.

matkocsis commented 5 years ago

I don't really "integrate" with SpringBoot. I've successfully used SB in projects. I have an example project doing just that at https://github.com/jpbelang/BeerTrader.

Hey!

I have seen that you have used uppercased letters for the enum declarations in your example. I is that intended? I am having problems with some of the default example RAML apis (eg.: the jukebox) using lower cased enum definitions. At runtime Jersey is having obvious problems not finding the enum constant:

Generated java code with lower cased enum def:

@XmlEnum
public enum AlbumsAlbumIdSongsGetOrder {
  @XmlEnumValue("desc")
  @JsonProperty("desc")
  DESC("desc"),

  @JsonProperty("asc")
  @XmlEnumValue("asc")
  ASC("asc");

  private String name;

  AlbumsAlbumIdSongsGetOrder(String name) {
    this.name = name;
  }
}

And the generated api looks like this:

  @GET
  @Produces("application/json")
  GetAlbumsResponse getAlbums(@QueryParam("query") String query,
      @QueryParam("orderBy") String orderBy,
      @QueryParam("order") @DefaultValue("desc") @Valid AlbumsGetOrder order,
      @QueryParam("offset") @DefaultValue("0") int offset,
      @QueryParam("limit") @DefaultValue("10") int limit);

And this will cause a runtime error:

java.lang.IllegalArgumentException: No enum constant com.example.AlbumsAlbumIdSongsGetOrder.desc

Also I have not found any bugs / issues regarding this java codegen problem. Have you ran into this?

jpbelang commented 5 years ago

Not too sure, but from a serialization perspective, it seems to work. I've put an example for it in the tool project. I'll check for the @DefaultValue.