Could Springfox generate different definitions for same class? - java

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.

Related

Different naming strategies in spring boot project for Rest and GraphQL APIs

I have a spring boot project with different Rest APIs and GraphQL APIs. Since the Rest APIs work with some old services, I need to set spring.jackson.property-naming-strategy to CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES. However, my GraphQL schemes work with the default naming strategy. It worked fine with graphql-java-tools v5. But after updating it to v12, the naming strategy for GraphQL schemes has also changed.
For example, a field with the name userId could be reached in Rest APIs with the name user_id and in the GraphQL APIs with the name userId. Now, both of them must be reached with the name user_id.
Since I can't change other services which use these APIs, I need to change the naming strategy for GraphQL. I tried #JsonNaming annotation to change the strategy for some classes, however, there are classes that are used in both APIs, so I can't use this for every class in my GraphQL requests and responses.
I could not find any config to change this naming strategy only for GraphQL. I also could not change the default property data fetcher in GraphQL, because I'm not sure how it works.

Is it possible to get the HAPI FHIR server to generate a StructureDefinition for a custom type defined with HAPI annotations?

I've got a number of extensions which I add locally to a base resource type. I do that using the technique described at http://hapifhir.io/doc_extensions.html under the heading Custom Resource Types.
For the server code and client code this is pretty convenient, because my extensions can be dealt with using nicely named accessor methods etc. My HAPI annotated classes are in a separate library that both server and client can use.
However is there a way to have the HAPI FHIR Server generate a StructureDefinition based on a class similar to the one defined at http://hapifhir.io/doc_extensions.html under the heading Custom Resource Types? I'd rather not create a StructureDefinition resource and the HAPI annotated class and have to keep them in sync, I'd rather just manage the HAPI annotated class and generate the StructureDefinition from that...if possible.
What I'm hoping for is for it to generate a StructureDefinition the same as the base resource plus the extensions with the specified cardinalities in the #Child annotation. Does something like this exist?

Difference between jersey-media-json-jackson, jersey-json,jersey-media-multipart?

I want to develop one web api,that will produce and consume both JSON and XML data.
I have used JAXB for XML support and it is working fine.
Now i want to add JSON type.i studied different tutorial, all are using different dependencies as below:
jersey-media-json-jackson
jersey-json
jersey-media-multipart
please help me whcih perfect and which support both XML and JSON.
what is difference between them all?
1. Jersey-Media-Json-Jackson
Jackson JSON processor could be controlled via providing a custom Jackson 2 ObjectMapper (or ObjectMapper for Jackson 1) instance. This could be handy if you need to redefine the default Jackson behaviour and to fine-tune how your JSON data structures look like. Detailed description of all Jackson features is out of scope of this guide. The example below gives you a hint on how to wire your ObjectMapper (ObjectMapper) instance into your Jersey application.
In order to use Jackson as your JSON (JAXB/POJO) provider you need to register JacksonFeature (Jackson1Feature) and a ContextResolver<T> for ObjectMapper,
2. Jersey-Json :
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T> providers distributed with jersey-json module. These providers enable using three basic approaches when working with JSON format:
POJO support
JAXB based JSON support
Low-level, JSONObject/JSONArray based JSON support
3. Jersey-media-multipart
The multipart in this module provide an integration of multipart/* request and response bodies in a JAX-RS runtime environment. The set of registered providers is leveraged, in that the content type for a body part of such a message reuses the same MessageBodyReader<T>/MessageBodyWriter<T> implementations as would be used for that content type as a standalone entity.
The following list of general MIME MultiPart features is currently supported:
The MIME-Version: 1.0 HTTP header is included on generated responses.
It is accepted, but not required, on processed requests.
A MessageBodyReader implementation for consuming MIME MultiPart
entities.
A MessageBodyWriter implementation for producing MIME MultiPart
entities. The appropriate #Provider is used to serialize each body
part, based on its media type.
Optional creation of an appropriate boundary parameter on a generated
Content-Type header, if not already present.

Different i18n in spring according to url

I have a spring web application that is required to work as following
the application will be accessed from two different URLs www.domain1.com and www.domain2.com
and it is required that the two URLs looks like two different applications with different CSS and I18n.
for the css part is done but I am stuck with the i18n part
How to make spring load different i18n properties file according to the domain name?
The solution that I thought in is to implement a filter that check the request URL and according to the URL it clears the message source bean and load the required i18n file but it does not looks good for the performance
by the way I am using ReloadableResourceBundleMessageSource message source
Another solution is to implement two different message sources.
The problem with this solution is that from the source code I can manage the bean that I use but how can I tell the fmt:message tag which data source to use ?
Thanks in advance and best regards
I suggest using a LocaleResolver. This is a standard Spring interface for doing exactly this sort of thing.
Interface for web-based locale
resolution strategies that allows for
both locale resolution via the request
and locale modification via request
and response.
This interface allows for
implementations based on request,
session, cookies, etc.
The pre-defined implementations of LocaleResolver don't do what you need, but it's trivial to write your own. Your implementation would be asked by to determine the locale for each request, and this information is then used by Spring's i18n code, including ReloadableResourceBundleMessageSource. You just declare your LocaleResolver bean in the context, and it's picked up automatically.
Your resource bundles would then use standard java locale mechanism to resolve the correct message for the current locale.

Spring 3 JSON with MVC

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.

Categories