How to convert swagger JSON objects into Java class objects - java

Sorry for my naive question, I am looking for some tools that would automatically convert JSON objects (that are generated in swagger UI) to a Java class objects in Eclipse.
Are there any such tools or plugins that can integrate swagger code with Eclipse Java and Groovy Grails framework. It would save a lot of time by not rewriting a huge amount of Java code every time when I want to change something in JSON objects.

I suggest to use autorest tool to generate code and models. It supports multiple languages including Java. Here is the github repository autorest
Just follow simple steps-
npm install -g autorest
autorest --input-file=https://******url******/swagger/v1/swagger.json --java --namespace=****namespace*****

You can use Google Gson Library in java.

You could use that or Groovys JSONsluprer but I depending on what you're trying to achieve it might just be easiest if you have a swagger to start with swagger codegen https://github.com/swagger-api/swagger-codegen tools and have it make those for you. If not you can always look to investigate how they did it.

Related

Spark Model to use in Java Application

For analysis.
I know we can use the Save function and load the Model in Spark application. But it works only in Spark application (Java, Scala, Python).
We can also use the PMML and export the model to other type of application.
Is there any way to use a Spark model in a Java application?
I am one of the creators of MLeap. Check us out, it is meant for exactly your use case. If there is a transformer you need that is not currently supported, get in touch with me and we will get it in there.
Our serialization format is solely JSON/Protobuf right now, so it is very portable and supports large models like RandomForest. You can serialize your model to a zip file then load it up wherever.
Take a look at our demo to get a use case:
https://github.com/TrueCar/mleap-demo
Currently no, your options are to use PMML for those models that support it, or write your own framework for using models outside of Spark.
There is movement towards enabling this (see this issue). You could also check out Mleap.

Compile a GWT library into a JavaScript library

I want to know what is the best approach to take to build a GWT library into a JavaScript library. When this GWT library to be compiled into Java script does not have any GWT server side component in it.
Just plain front-end components. Including only things like AJAX calls, etc.
In Java the GWT library is used like this:
TheGWTLibrary api = new TheGWTLibrary();
api.setServer("http://www.somewhere/api");
api.post(stuff, new Callback(){
void success(){
}
void fail(){
}
});
api.get(new Callback(){
void success(){
}
void fail(){
}
});
Such that the GWT library project would be usable in any HTML project without making it a GWT app.
If this is possible where will the dependencies of this GWT library be compiled to? Will it be included in a one single JavaScript (js) file?
[EDIT 2016]
The way to go with new GWT (2.8.0) is using JsInterop, read the documentation API. Note that it's still in 2.8.0-SNAPSHOT but will be released in few weeks. Also, JsInterop in 2.7.0 has been deprecated and removed in 2.8, so don't use it.
[END EDIT]
Apart from writing your JSI method, there are two easy ways to deal with this.
1.- GwtExporter
You might be interested on reading this article I wrote some years ago:
https://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en
It uses gwt-exporter, and you can take a look to a couple of projects using this approach:
JsUpload wich is a port of gwtUpload:
https://code.google.com/p/gwtupload/wiki/JsUpload_Documentation
https://github.com/manolo/gwtupload/tree/master/jsupload
GwtChismes is a very old library useless right now and not maintained anymore, but it is exported as well
https://code.google.com/p/gwtchismes/wiki/JsChismes_Documentation
https://code.google.com/p/gwtchismes/source/browse/#svn%2Ftrunk%2FGWTChismes%2Fsrc%2Fjschismes
I also did some experiments exporting gwtquery to js (jsQuery):
https://code.google.com/p/gwtquery/wiki/JsQuery
Chronoscope was another gwt library using this approach
https://code.google.com/p/gwt-chronoscope/wiki/JavaScriptAPI
2.- JsInterop
If you want to play with new stuff, you might read about JsInterop a beta feature in GWT-2.7 which will be fully functional in GWT-3.0.
It allows you to export java classes to JS. You have to use the -XjsInteropMode JS and some annotations.
There is no so much documentation right now, but there is a document explaining the API, and an interesting presentation.
https://docs.google.com/document/d/1tir74SB-ZWrs-gQ8w-lOEV3oMY6u6lF2MmNivDEihZ4/edit
http://gokdogan.appspot.com/gwtcreate2013/#1
It sounds like you're looking for something like GWT Exporter. It allows you to export a GWT library as a publicly accessible javascript API.
It looks like you'll still need to compile the GWT library as a web application to use it, but if you don't have any server-side code, it should be as simple as compiling your API, copying the war folder and adding a reference to war/[yourapp].nocache.js.
If that's not what you're looking for, you can check out this question for other options.

Grails and Java: Proper way to unmarshall Json from Grails in Java app

I'm quite new to Grails things. here the problem:
I created a simple class exposed as a REST service with grail. (I only use grail for CRUD operation on database).
I have an Java FX application that needs to consume this service to recreate the original object. Is there a clean way to "export" domain class from grails ?
Thx in advance for any help !
You can create common java library for both projects, include in Grails app and JavaFX app. This common library will share common classes. For marshalling/demarshalling JSON you can use Jackson
GSON or Jackson will do what you want.
Take a look at http://grails.org/doc/latest/guide/webServices.html#domainResources.
For example, if you are returning a "book" domain class from your REST service you could use:
render book as JSON

Is there something like JET (Java Emitter Templates) but without Eclipse-dependencies?

I wanted to use JET (Java Emitter Templates)
in my Netbeans projects, but had to find out that JET
heavily depends on Eclipse libraries.
Is there something similar to JET, but as a standalone project?
Something which is open source and well maintained?
Futhermore, is "code generation" the common term for such tools?
If you are using Maven, you can use JET templates with the maven-jet-plugin.
This seems to be the source code. And here the documentation.
It is not actively maintained but works pretty well and follows the JET spec.
I've used it with templates and skeletons.
It's self contained, doesn't depend on Eclipse, and doesn't introduce any transitive dependencies to your project.
Indeed JET is very tied with Eclipse.
If you want to generate code without Eclipse you should use another solution.
You can choose another template engine like Velocity (https://velocity.apache.org/) or FreeMarker (https://freemarker.apache.org/).
Or you can choose a code generator working by itself independently of any IDE.
For example "Telosys Command Line Interface" : http://www.telosys.org/
From what I know, JET is something like JSP, no?
Java Emitter Templates are very similar to Java Server Pages (JSPs). Both JETs and JSPs use the same syntax, and are compiled to Java behind the scenes. Both are used to separate the responsibility for rendering pages from the model and controller. Both accept objects passed into them as an input argument, both allow inserting string values within code ("expressions"), and allow direct use of Java code to perform loops, declare variable, or perform logical flows ("scriptlets"). Both are good ways of representing the structure of a generated object (web page, Java class, or file) while supporting customization of the details.
JETs differ from JSPs in a few key ways. In a JET, the structure of the markup may be changed to support generating code in different languages. Typically the input to a JET will be a configuration file and not user input (though there is nothing forbidding this). And also typically, JET processing will take place only once for a given workflow. These are not technical limitations, and you may find uses for JETs which are quite different...
ibm.com
Here are a few links to get you started on JSP, if that sounds like what you need:
sun.com
netbeans.org
Look for "template engine" for these types of tools.
A couple you might want to look at:
Apache Velocity (http://velocity.apache.org/)
StringTemplate (http://stringtemplate.org/)
I ended up using ERB (Ruby's template engine).
Works great in Netbeans!
I define custom ant task which generates source files by calling ERB (whose results are placed inside a non-versioned special directory).
The ant task is overriding Netbeans' "-pre-compile" task.

Code / template generator for Java

For C#, I have often used CodeSmith and lately the T4 generator which is part of Visual Studio.
I'm looking for something similar for Java, in particular an Eclipse add-in since I do all my Java development using the Eclipse IDE.
I've found that freemarker does a pretty good job for generating any type of code. From the website:
FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.
It is used by struts2 a lot. The website has a long list of other products that use freemarker.
I have worked with both Velocity and StringTemplate. Velocity is a bit more conventional (think JSP/ASP concepts), while StringTemplate seems a bit cleaner. in a sense described in this Artima interview. Both are pure templating engines, and will require you to write some code around them, if you want to do full-blown code generation from a model.
StringTemplate is used internally by ANTLR, which may be useful to your effort.
As an alternative approach, you could go with Eclipse, using EMF and JET.
You should try Telosys Tools, an Eclipse plugin for code generation working from an existing database with customizable Velocity templates
See: http://www.telosys.org/
Eclipse Marketplace : http://marketplace.eclipse.org/content/telosys-tools
The tutorials are here : https://sites.google.com/site/telosystutorial/
See this other question about CodeSmith : Is there any freeware tool available which is like Codesmith?
I use JavaForger to generate code from templates. It parses existing classes so that you can use that class-data inside your templates. It can both create new classes or insert code into existing classes. You can determine where generated code will be inserted based on a string conversion rule (e.g. myProject/dao/ProductDao.java => myProject/service/ProductService.java).
JavaForger is open source and uses FreeMarker as template engine and JavaParser as parser.
This is an old question but the only thing that comes close (for Java) to do what CodeSmith Generator does is Spring Roo.
The reason is that Java does not have Partial Classes like C# does. Spring Roo gets around this by using AspectJ ITDs.
My answer is to use StringTemplate, but there is a bit more to it than just what tool to use.
Is it the issue to generate java code? Or is it to use java tools? Programmers would be normally very comfortable writing code. Therefore, it would not be a leap to write some java classes and write a walk that would generate code using StringTemplate. I personally think it is a good exercise to create example models, generate your java code from the models. And depending on your use case you could end up writing JSON models by hand and never having to write any java code to produce the java code. Or you could end up writing Java classes that produce equivalent models.
You could use the StringTemplate based STST, which reads JSON. STST is command line based, and I am sure you could hook it to both eclipse and/or Visual Studio.
I personally think about portability, JSON is an extremely simple language. And almost every language has libraries that support it.
I'm not a C# man so I don't know what the equivalents would be, however I've found xdoclet to be very good in the past. I don't think it integrates with eclipse as such but you can run it from an ant script. Does things like generating Hibernate mapping files from annotated Java classes. Useful if that's what you're looking for :)
eclipse has a built-in template system.
look in window -> preferences -> java -> code style -> code templates
You might look at my plugin : http://fast-code.sourceforge.net/. It allows one to select multiple fields and generate code using user specified velocity templates.
Take a look at my project https://github.com/karajdaar/templator and see if that helps.
I wrote a simple web based application for my use.
its available at https://github.com/harish2704/templates
and a demo is available at http://templates-harish2704.rhcloud.com/
Its language independent tool. GUI supports several languages ( highlighting, snippet completion ect )

Categories