By default, Swagger parses a class's data members in order to document the objects used as parameters or returned by a given web service. If you're using Jackson, the Jackson annotations provide a much more accurate description of the API.
Does anyone know an (easy) way to get Swagger to parse Jackson annotations. Perhaps an overridden parser?
Not sure if this would help, but Jackson 2.1 and later expose POJO structure as seen by Jackson itself (ObjectMapper.acceptJsonFormatVisitor), which could be used for generating different kinds of artifacts. I have written an Avro schema generator with it, for example (as part of Jackson Avro module)
As of version 1.2, Swagger can parse Jackson annotations on its own. I confirmed this using Jackson 2.1.
Related
Is there any java/python/javascript library that validates API request/payload against openapi 3.0.2
i looked many of the libraries but was not able to find the correct one.
There is one problem which is found when these libraries are not able to validate polymorphism and inheritance. for example oneOf, anyOf, allOf.
what is being trying to do is to validate an API request, which includes a set of fields as described by using oneOf keyword and then followed by discriminator & mapping. The mapping fields include values with allOf Keyword but the request for validating swagger fails.
I would suggest the OpenAPI Initiative Tooling page for the list of available libraries.
There's Data validators category.
A half year ago, the best validator library I found for Java was Atlassian's Swagger Request Validator.
I want to parse JSON data from a RESTful service.
Unlike a SOAP-based service, where a service consumer can create stubs and skeleton from WSDL, in the case of the RESTful service, the service consumer gets a raw JSON string.
Since the service consumer does not have a Java object matching the JSON structure, we are not able to use the JSON to Java Mappers like GSON, Jackson etc.
One another way is to use parsers like JsonPath, minimal-json, etc which help traversing the JSON structure and read the data.
Is there any better way of reading JSON data?
The official docs for Jackson mention 3 different ways to parse a JSON doc from Java. The first 2 do not require "Java object matching the JSON structure". In Summary :
Streaming API (aka "Incremental parsing/generation") reads and writes JSON content as discrete events.
Tree Model provides a mutable in-memory tree representation of a JSON document. ObjectMapper can build trees that consist of JsonNode nodes.
Data Binding converts JSON to and from POJOs based either on property accessor conventions or annotations.
With simple data binding you convert to and from Java Maps, Lists, Strings, Numbers, Booleans and nulls
With full data binding you convert to and from any Java bean type (as well as "simple" types mentioned above)
Another option is to generate Java Beans from JSON documents. You mileage may vary and you may/probably will have to modify the generated files. There are at least 5 online tools for that purpose that you can try:
http://www.jsonschema2pojo.org/
http://pojo.sodhanalibrary.com/
https://timboudreau.com/blog/json/read
http://jsongen.byingtondesign.com/
http://json2java.azurewebsites.net/
There are also IDE plugins that you can use. For instance this one for Intellij https://plugins.jetbrains.com/idea/plugin/7678-jackson-generator-plugin
The GSON supports work without objects, too. Something as this:
JsonObject propertiesWrapper = new JsonParser().parse(responseContent).getAsJsonObject();
assertNotNull(propertiesWrapper);
propertiesWrapper = propertiesWrapper.getAsJsonObject("properties");
assertNotNull(propertiesWrapper);
JsonArray propertiesArray = propertiesWrapper.getAsJsonArray("property");
assertNotNull(propertiesArray);
assertTrue(propertiesArray.size()>0, "The list of properties should not be empty. ");
The problem is that the work this way is so inconvenient that it is really better to create objects instead.
Jackson has absolutely the same problems, and to greater extent - extremal inconvenient for direct json reading/creation. All its tutorials advice to use POJOs instead, too.
The only really convenient way is use Groovy. Groovy works as an envelope on Java, you can simply write Java code and use Groovy operators at need. And in JSON or XML reading and creation Groovy is incomparably more powerful that Java with all its libraries multiplied on each other! It is even much more convenient than already prepared by somebody else tree structure of ready POJOs.
If I used Autobean on GWT client to serialise my POJO (sent out thro RequestBuilder) but I plan to use say, groovy, perl or php to service that request, I would need to know the serialization format of Autobean.
What is and where can I get the JSON format spec for Autobean?
They're documented here.
My object gets serialised as {name="nyname" ....}, however, I require all keys to be start with uppercase, ie. {Name="myname",.....} how can I do that with Jackson ?
I had the same issue and posted about it earlier. I solved it here:
How can I enable Pascal casing by default when using Jackson JSON in Spring MVC?
If you're not using Spring, the answer would be similar. You'll still need a custom (de)serializer, and you'd just need to wire it in to your program in the manner you choose.
I want to convert my pojo to json with JAXB, my pojo have one to many relation, and when i convert my pojo to json, JAXB generate error "A cycle is detected in the object graph. This will cause infinitely deep XML".
I read from web that, this problem can be solved with help from #XmlID and #XmlIDREF, but there is one problem, my Id attribute is not String type but Long. and as far as i know #XmlID can be used only with String property.
Other web suggest using eclipselink MOXy, but MOXy cannot generate json.
As you mentioned in your question EclipseLink MOXy (I'm the tech lead) has the #XmlInverseReference annotation to solve the problem of bidirectional relationships. As of EclipseLink 2.4 MOXy can produce/consume JSON.
For More Information
http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
You have a cyclic reference problem in your definition.
Try putting #XmlTransient above the problematic definition.
Also, about XmlID and string type, see http://markmail.org/message/up6vrzjixxrvy5th.
The JAXB specification requires that the property marked with #XmlID be a String property.
MOXy impl allows to use long.
One hack to keep using full JAXB compliant implementation would be to duplicate your id in a String field (before serialising)
Don't know so much about JAXB but XStream makes you able to use different modes and some of these modes will give references to the xpath address (absolute or relative) of an element in your xml, if these elements are already displayed.
(And you can do Json with XStream)
I faced similar problem when I wanted to convert my POJO to JSON with JaxRS. The MoxyJsonProvider is the default option of eclipselink but it fails to parse the JSOG (where cycles exists in the JSON structure). Jackson Jaxb Provider does this better with ObjectMapper.
I have elaborated in this answer below, about how to invoke Jackson Provider instead of Moxy. You will need jackson packages in your pom xml.
https://stackoverflow.com/a/60319306/5076414