I have using Play framework and been exposing REST APIs accepting XML Request.
Can anyone help with efficient way of parsing XMLrequest into Java POJOs at controller level while accepting request.
There are few ways of parsing them using JAXb libraries by unmarshalling. I am interested in efficient way if there are any play framework provides.
Related
I hope my question is not too broad.
Our application has an Angular frontend and a Java backend and
we need both frontend and backened validation. At the moment, we manually code
validators in Typescript and Java that basically do the same thing.
Since that is not only tedious but also error-prone, we would like to define once (maybe in a JSON file)
which input fields have which constraints and ideally use a piece of software that auto-generates
Java and Typescript code based on that definition.
My question is: are there any open-source/commercial software solutions that take care of this?
It seems like a common enough problem, but I haven't come across
a proper solution yet. JSON schema seemed promising and since we're using swagger
anyway, I'd hoped that swagger-codegen would automatically take the supported JSON-schema keywords as constraints into account,
but I haven't figured out how it works. When I generate Angular code, constraints such as 'minimum' and 'maximum' are completely ignored,
and when I generate Java SpringBoot code, they only show up as annotations.
This is exactly what JSON Schema is designed for. Open API (swagger) modifies JSON Schema for it's own purposes, which doesn't include validation.
JSON Schema has validator implementations in most major languages allowing you to use the same schema to do the same validation on the front end and the back end. http://json-schema.org/implementations.html
Unfortunately, you likely won't be able to use the same schemas you use for your Open API service definition. I'm not aware of any validator implementations that support Open API's flavor of JSON Schema. I understand that Open API is trying to close the gap between their definition and JSON Schema, but a gap still exists.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am coming from Node.js background and have quite a good understanding of RESTful web services.
Now I am trying to build RESTful web services using Java. I understand core Java but completely new to Java based web development.
I come to conclusion after some tutorials that I need to use Jersey framework to build my RESTful API. I understand that Jersey is some sort of reference implementation of JAX-RS.
But I fail to understand relationship between various other terms and components like JAXB, Jackson, EclipseLink Moxy, jersey-media-moxy, Jettison, JSON-P JSON, XML, etc. that come across my readings. The only thing that I could conclude was it is not so straight forward like JavaScript to covert Java objects into XML or JSON equivalent.
My question is what is the relationship between these terms mentioned above and how they fit together if I am developing a Java based RESTful API.
There sure is a lot of terminology in the Java world and that can create a significant learning curve for new developers. It's not that it's particularly difficult to pass JSON or XML documents around using Java, it's just that the various bits and pieces you need to do it have sprouted terminology over the years. I've tried to list my understanding of the terms you've used below...
XML - you know what XML is, right? The extensible markup language. It's what we had before JSON became the big thing.
JSON - oh, well, JSON is the new big thing. It's a human readable object serialisation format, less verbose than XML. Very popular nowadays. It's the new magic bullet, good for what ails ya, gonna solve all your problems...
JAXB - the "Java Architecture for XML Binding" in the Java ecosystem is the primary mechanism for turning XML data into objects which you can then interact with, and vice versa. It's important to realise that it's an API and not an implementation, so it mostly defines a set of annotations and simple classes / interfaces in the javax.xml.bind package. To do anything useful with JAXB you need an implementation. There's a reference implementation included in the Glassfish application server. Most application servers will have some kind of implementation of JAXB.
Jackson - a library for data binding. It supports both XML and JSON as document formats, and implements the JAXB API. You can use Jackson as your implementation of JAXB, or you can just use the Jackson API directly.
EclipseLink Moxy - an alternative implementation of the JAXB API. Like Jackson, it also has its own API. You can choose to use it, or not. You probably don't want to use both Jackson and Moxy.
Jersey-media-moxy - as you mentioned, Jersey is an implementation of JAX-RS. One aspect of JAX-RS is passing documents around - often XML or JSON. To do that Jersey needs to know what underlying library to use for data-binding or stream processing. So jersey-media-moxy exists as a kind of jersey plugin dependency which you can use to configure Jersey to use Moxy for your object serialisation needs. There's an equivalent package for using jackson called jersey-media-json-jackson.
Jettison - Yet Another serialisation library for converting Java objects to Json and back.
JSON-P - an API for processing JSON either as a stream of events or via data-binding to an object. This API is still in development. You might ask how it is that anybody does json processing without it - the answer is that they either utilise proprietary library APIs (such as Jackson or Moxy) or they use a library which repurposes the JAXB API to work with JSON (Jackson definitely allows this, I'm not sure about Moxy). JSON-P will make it easier to work directly with JSON features, without all the XML-concepts which JAXB brings in.
Please bear with me for this novice question.
I am calling a RESTful web service APIs that returns XML response. Apart from normal XML parsing schemes like DOM based parsing, SAX based parsing, is there a way to transform this XML response directly into some object? What more details/specification from service side would be required to do such transformation?
i can't give a summary of all the options available, but i recently used jaxb to do the opposite (java to xml) and it was simple and easy to use. since jaxb also supports xml to java, as described here, i would suggest giving that a look. it's based on annotations and java beans (or pojos) - you just indicate which attributes correspond to the elements with attributions, and it does the rest.
if you have a schema, it will generate java classes for you. alternatively, here's an example of working without a schema.
ps according to comments in the final link, you don't even need to annotate if the names match the xml!
I'm newbie to XML using Java. I've to write a method to send a large XML data having lots of nodes through a socket to client application.
What is the suitable method to generate XML?
What is the best method to send large XML through sockets?
Since you are using sockets you just need to deal with Java InputStream/OutputStream. This gives you alot of flexibility in your XML handling as almost all XML technologies handle streams as input/output.
You could represent your data as plain old Java objects (POJOs), and then bind them to XML using JAXB. An implementation of JAXB is included in Java SE 6. There are other implementations such as MOXy (I'm the tech lead) and JaxMe.
For an example see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
To generate XML you use DOM implementation provided by any XML DOM parser and generator.
Here is a nice tutorial. But for only generation try to use some small and light-weight parcers e.g. [tinyxml][2] or [qdparcer][3], because the xerces and others are going to be heavy weight for that. But if the parcing is also involved libxml or xerces will be of good choice because they provide nice SAX implementation for parsing, but you need to have schema defined for your data. Again try to serialize the data before sending so you can get rid of other problems.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to test my RESTful applications directly via HTTP and I am looking for tools that can help me with that task. Basically I am looking for an easy wrapper for HTTP requests that can submit e.g. HTML forms or serialized resources as JSON or XML.
It would be great if there is a way to verify if the service is actually following REST architectural guidelines (statelessness, URIs, content negotiation etc.), too.
Being able to use it with JUnit would be a convenient bonus. Do you know about any libraries that could help me with what I want to do (and that are a little more than just a simple http client)?
See if rest-client is of any help.
Edit: Currently I am using Postman - REST Client a google chrome plugin and it's awesome!
I think REST Assured will suite you very well. It's very easy to send requests and to parse XML and JSON responses. E.g. let's say that a GET request to "/lotto" returns JSON:
{
"lotto":{
"lottoId":5,
"winning-numbers":[2,45,34,23,7,5,3],
"winners":[{
"winnerId":23,
"numbers":[2,45,34,23,3,5]
},{
"winnerId":54,
"numbers":[52,3,12,11,18,22]
}]
}
}
You can make the request and validate the response like this:
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
There is also the Jersey Test Framework (http://jersey.java.net/nonav/documentation/latest/user-guide.html#test-framework) but as Johan already mentioned the REST-assured framework I'd also recommend this framework - it has some nice featues like a DSL like syntax, XPath and Schema validation, easy file upload and using Groovy Lambda Expressions to search through returned JSON structures..
I have written two articles..
the first one compares REST-assured and Jersey-Test-Framework (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/),
the second explores the features of the REST-assured framework against a given REST service (http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/)
Fiddler is a really useful tool, you can create XML based HTTP Requests with a variety of request verbs like GET,POST,PUT,DELETE and so on.
http://www.fiddler2.com/fiddler2/
Maybe Selenium can be of some help, but surely not entirely.