I write OPENAPI 3.0 yaml file as the documentation for java REST api project. It can be nicely rendered with ReDoc. And in order to help other developers and our customers to use the API I should always keep the java response java the same with the OPENAPI yaml schema definition.
So...it there some tools to help use the OPENAPI yaml schema validate JAVA Response?
I have seen some tools like rest-assured can use json-schema for response validation. But it seems that the OPENAPI schema is a little bit different from the json validation. I can not use it directly.
I solved this problem by using https://github.com/mikunn/openapi2schema. This package help translate openapi definitions into valid json schema draft 4 version. And I can then use rest-assured to validate the json response with this json schema file.
I've been using Springfox-swagger2:2.3.2 and Springfox-swagger-ui:2.3.1 in one of my projects and I've run into an issue.
The project is in Spring Boot 4.2.4 with JSON HAL standard.
Basically we use the same resource class for a POST body and the response body. Since it is HAL, this resource has links and embedded fields. In the POST definition we don't want the links and the embedded fields, but they should be there in the response definition.
Is it possible for Springfox to generate different definitions of this resource so that the POST Model Schema does not contain the links and embbeded fields?
I didn't want to have to create different classes of the same resource for each context that it is used. I've tried using the 'hidden' attribute in the #ApiModelProperty annotation, but that ended up removing the links and embedded fields from the response Model Schema.
I use TomEE 7.0.1 with Jackson 2 as JAX-RS JSON provider (configured in openejb-jar.xml). Posting json to jax-rs services works perfectly well and uses the jackson annotations.
But using JAX-RS client (or cxf webclient) always uses Johnzon even if I provide Jackson to the Webclient.create method. After some debugging I am sure that TomEE adds Johnzon to the client factory somehow and no other message body parser for json can be used.
Is this a bug with the TomEE CXF integration or do I miss something?
Johnzon is registered on the bus by default to have a default JSON (mapping) and JSON-P provider. CXF is clever enough to make bus providers with a lower priority than applications ones (register() for client API) so if you call register you expect to use jackson...and still use johnzon - I suspect it is the case you hit.
That's perfectly normal and due to two things:
JAX-RS specification priority definition
Jackson Consumes/Produce definition
To summarize (1) says that more specific is the provider higher is its priority so an "application/json" provider will be priveledged in favor of a "/" one.
To avoid issues Johnzon uses "application/json". However jackson uses a custom matching strategy and therefore uses "/". So the bus priority is ignored since mediatype priority is enough to say johnzon is "more adapted" than jackson for json.
To solve it the easiest is likely to override jackson provider (just extend it) and decorate it with #Provides/#Consumes with MediaType.APPLICATION_JSON instead of wildcard one.
In my java code i am getting an xml response (without an xsd) from a third party service call. But I know what possible tags can come in the response.So I have manually created java bean classes and annotated it with jaxb syntax, and them I am unmarshaling the got response to objects. It's working fine. But I have some scenarios where I will get french content in between the xml tags. In those cases it fails to unmarshal. So, how should I proceed?
Is there a way to build Spring Web calls that consume and produce application/json formatted requests and responses respectively?
Maybe this isn't Spring MVC, I'm not sure. I'm looking for Spring libraries that behave in a similar fashion to Jersey/JSON. The best case would be if there was an annotation that I could add to the Controller classes that would turn them into JSON service calls.
A tutorial showing how to build Spring Web Services with JSON would be great.
EDIT: I'm looking for an annotation based approach (similar to Jersey).
EDIT2: Like Jersey, I am looking for REST support (POST,GET,DELETE,PUT).
EDIT3: Most preferably, this will be the pom.xml entries and some information on using the spring-js with jackson Spring native version of things.
In case other ppl get here later:
http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
was the most useful link for me. That finally made me understand the model (after less than a day of searching).
You can certainly have controllers that return a View which is in JSON output. Take a look at the Spring-JSON project.
To consume requests in JSON I would assume you would just want a controller to pass a request parameter off to a JSON library which could parse the data?
There is no pre-packaged way to do what you want as Jersey is nicely integrated with Spring via the Jersey-Spring API so there's really no reason to re-invent the wheel.
Check this one out
Adding support for JSON and XML views
Source code for Spring Finance Manager
Since spring-mvc 3.0 official support for Ajax remoting with JSON is provided as part of Spring MVC. This includes support for generating JSON responses and binding JSON requests using the Spring MVC #Controller programming model.
see here
This feature is now part of Spring since version 3.0. You can simply use the #ResponseBody annotation to specify that you want the return value from your request handler methods to be serialized to JSON and sent as the response body. See http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
It seems that DWR framework looks very close to what you want to get.
You can configure Spring MVC interceptors through handler mappings and then use the mappings to transform the returning data to JSON. The handler configuration can be done in xml (no need to recompile), and this can be completely transparent to the rest of the application.
It's fairly easy to annotate controllers with some annotation, and then hook up the BeanFactory bootstrap process to register the controllers within some handler mapping process.
I have used this approach to transform the result from spring controllers into GWT RPC calls.
You can also use Spring AOP to intercept controller method calls and unwrap/wrap the requests from/to JSON.