phoenixnap / springmvc-raml-plugin

Spring MVC - RAML Spec Synchroniser Plugin. A Maven plugin designed to Generate Server & Client code in Spring from a RAML API descriptor and conversely, a RAML API document from the SpringMVC Server implementation.
Apache License 2.0
136 stars 84 forks source link

Generate Spring MVC Endpoints from RAML file #3

Closed kurtpa closed 8 years ago

kurtpa commented 8 years ago

Add goal to generate SpringMVC Code from a published RAML file

kurtpa commented 8 years ago

Experimental generator support added to version 0.2.1 currently on master

erreobi commented 8 years ago

Hi kurtpa,

I downloaded the version 0.2.1 from the "develop" branch, but I didn't find the instruction to use it. I need to rasform an existing RAML to SpringMVC. could you please help me on that?

Thansk for your plugin!

Regards, Rob

kurtpa commented 8 years ago

Hi rob!

I'm currently away on vacation so I cannot post a code snippet to you here

There is a new class in 0.2.1 in the parser subproject called RamlParser. Simply create a main method, load the Raml model from file and then invoke the parser's to get the set of ApiControllerMetadata

Once you do that, there is a new method in the RamlGenerator which is called generateClassForRaml and call this for each ApiControllerMetadata. Save the string it will give you to file.

If you want I'll be in the office tomorrow and can send you a code snippet for the above. I plan to finalise development of this feature by next week

Regards

Kury

Sent from Outlook Mobilehttps://aka.ms/blhgte

On Wed, Jan 20, 2016 at 10:25 AM -0800, "erreobi" notifications@github.com<mailto:notifications@github.com> wrote:

Hi kurta,

I downloaded the version 0.2.1 from the "develop" branch, but I didn't find the instruction to use it. I need to rasform an existing RAML to SpringMVC. could you please help me on that?

Thansk for your plugin!

Regards, Rob

Reply to this email directly or view it on GitHubhttps://github.com/phoenixnap/springmvc-raml-plugin/issues/3#issuecomment-173315127.

kurtpa commented 8 years ago

Glad to hear it worked for you :)

Yes, the response body and request body object generation is what I'll be working on this afternoon :)

Best regards

Kurt

kurtpa commented 8 years ago

Latest commit to develop (74523b08de5f9d3d04e13cacee40bb29526c0270) adds new functionality to support most of the issues above.

Examples are still pending i'm afraid

erreobi commented 8 years ago

It continues to create the methods that return the ResponseEntity, do you know why? It should be also create the Objects for the response following the schemas, but it didn't. are you adding also this functionality?

Thanks a lot!

Rob

kurtpa commented 8 years ago

can you paste a snippet of the RAML you are using? (or send it via email if you prefer)

If the successful response (200) has a body mime type containing "json" and a Json schema, it should use it as the Response object. Only if no body is found will the ResponseEntity be used

erreobi commented 8 years ago

Here is my example raml: BooksAPIs.zip

Please let me know.

kurtpa commented 8 years ago

Latest commit should address most if not all of your issues based on the example you supplied :)

erreobi commented 8 years ago

great!! What I can produce is:

import org.springframework.http.*; 
import java.util.*; 
import org.springframework.web.bind.annotation.*; 
import com.rob.test.model.*; 

/**
 * Returns the list of books
 */
@RestController
@RequestMapping("/books")
public class BookController { 

    /**
     * No description
     */
    @RequestMapping(value="", method=RequestMethod.GET)
    public @ResponseBody ArrayList<BooksResponse> getBooks () { 

         //TODO Autogenerated Method Stub. Implement me please.
         return null;
    }

    /**
     * No description
     */
    @RequestMapping(value="/{title}", method=RequestMethod.GET)
    public @ResponseBody ArrayList<BooksResponse> getBookByTitle (@PathVariable String title) { 

         //TODO Autogenerated Method Stub. Implement me please.
         return null;
    }

}

how can I produce the BooksReponse class?

Rob

kurtpa commented 8 years ago

It should be produced automatically in the x.x.x.model package/directory where x.x.x is your basePackage as supplied to the plugin.

The Endpoint generation is now supported via Maven - if you refer to the README.md usage 3 (https://github.com/phoenixnap/springmvc-raml-plugin/blob/develop/springmvc-raml-plugin/README.md) there is a snipped you can paste into your pom. Let me know if this still doesnt produce the BooksResponse.

erreobi commented 8 years ago

yes, it works. There is a details very weird, it create the following classes:

image

the BookController classes is created in a file named spring\BookController.java.

erreobi commented 8 years ago

Another question: could it generate for Spring version 3.0.1?

kurtpa commented 8 years ago

I used this configuration and i didnt get the spring\ appended to the BookController.

<configuration>
        <ramlPath>/src/main/resources/BooksAPIs/api.raml</ramlPath>
        <outputRelativePath>/src/generated</outputRelativePath>
        <addTimestampFolder>false</addTimestampFolder>
        <basePackage>com.gen.wow</basePackage>    
</configuration>
kurtpa commented 8 years ago

Regarding Spring version 3 - i'll add an enhancement task. Off the bat i think the only change required would be to use @Controller instead of @RestController

erreobi commented 8 years ago

The generated code works very fine with Spring 4.1.0.RELEASE and FasteXml 2.7.0.

Unfortunately we are using the Spring version 3.1.0.Release and it is incompatible with Jackson2 (http://stackoverflow.com/questions/10420040/jackson-2-0-with-spring-3-1).

Have you planned to generate the code using Jackon version 1?

kurtpa commented 8 years ago

I've added an issue to support older versions of spring. The jackson annotations in model objects are added by the JsonSchema2Pojo module when generating classes from schemas. I beleive there should be a toggle to turn this off.

As for the server endpoints i can add Controller instead of RestController.

If you need this soon and want to implement this and submit a pull request you are more than welcome.

kurtpa commented 8 years ago

0.2.1 released with Spring Endpoint generation - work on testing and improving this module is still ongoing but with the release it will be easier for people to try it and give feedback :)

erreobi commented 8 years ago

I'm not an expert of maven plugin, I fixed it for my purpose but I think it needs to be improved adding a parameter. You can change the Jackson Annotator in the SchemaHelper class (line 378):

        SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson1Annotator(), schemaStore),
                new SchemaGenerator());

you can choose between Jackson1Annotator or Jackson2Annotator.

I hope it is useful. In the meanwhile I'm going to read some "quick" tutorial like "Hoe to write a maven plugin" :grin:

kurtpa commented 8 years ago

i'm adding this note to the issue regardnign support for multiple Spring versions. I'm also going to be closing this issue since support is now added in 0.2.1

mahulivishal commented 8 years ago

Can i get the Plugin Repository for com.phoenixnap.oss:springmvc-raml-plugin:x.x.x ?