Spring Integration REST service consuming both XML and JSON - spring-integration

I would like to implement the following simple use case with Spring Integration v4.2+:
A REST endpoint is provided to clients, for example (/api/person).
The clients can POST data to this endpoint. The data can be submitted in JSON, XML or Google Protocol Buffers format.
The application accepts the data and saves it to a database table.
I have the following Spring Integration configuration that allows the endpoint to accept JSON data:
<int:channel id="receiveChannel"/>
<http:inbound-channel-adapter id="restInputAdapter"
channel="receiveChannel"
path="/api/person"
request-payload-type="java.util.Map"
supported-methods="POST"/>
<jdbc:outbound-channel-adapter id="jdbcOutputAdapter"
channel="receiveChannel"
data-source="dataSource"
query='INSERT INTO "person" ("first_name", "last_name", "email_address") VALUES (:payload[firstName], :payload[lastName], :payload[email])'/>
This is in a Maven project with Jackson2 on the classpath, which is why no special configuration is required for reading JSON from the request.
However, attempting to post XML data results in the following exception:
org.springframework.messaging.MessagingException: Could not convert request: no suitable HttpMessageConverter found for expected type [java.util.Map] and content type [application/xml]]
I have tried using the message-converters attribute with http:inbound-channel-adapter, specifying Jackson, JAXB and Protocol Buffers converters explicitly but that does not help either.
I know how to do this in a bespoke Spring MVC application with #RestControllers so I am not looking for solutions outside of Spring Integration.
How can the Spring Integration configuration above be tweaked so that the REST endpoint can be made to accept JSON, XML and Google Protocol Buffers at the same time?

It seems that although JSON converts to java.util.Map, XML does not. This is why the seen error.
I created a class:
#XmlRootElement class Person { ... }
and then modified the configuration as follows (only changes shown):
<http:inbound-channel-adapter ...
request-payload-type="Person"
.../>
<jdbc:outbound-channel-adapter ...
query='INSERT INTO "person" ("first_name", "last_name", "email_address") VALUES (:payload.firstName, :payload.lastName, :payload.email)'
.../>
I can now post both XML and JSON data to the endpoint.
Google Protocol Buffers support can now be added using the Protostuff library.

Related

OpenAPI Generator issue with Destination service API specification

I want to get all destinations on subaccount and instance level. In SAP API business Hub, I found the API information and "SAP Cloud SDK" tab to generate code by OpenAPI generator.
https://api.sap.com/api/SAP_CP_CF_Connectivity_Destination/overview
I downloaded the API specification and added dependencies into Cloud SDK for Java project. The code is generated successfully with some errors (unknown models)in generated api classes.
For example in DestinationsOnSubaccountLevelApi.class, model OneOfDestinationNameOnly is imported and used in method but it is not generated in model package.
I looked into API specification and found that there were two types of response entity. That is the reason why the code could not be generated properly. I can modify the API specification to make it work but it should not be the long term solution. Is there any other way to fix this issue?
Unfortunately the SAP Cloud SDK Generator for Open API services is not yet able to understand oneOf relationship that is modeled in the specification.
As an alternative, would you consider using the DestinationAccessor API for resolving single destinations?
You can also directly instantiate an ScpCfDestinationLoader, which allows for querying all destinations:
ScpCfDestinationLoader loader = new ScpCfDestinationLoader();
DestinationOptions options = DestinationOptions
.builder()
.augmentBuilder(ScpCfDestinationOptionsAugmenter.augmenter().retrievalStrategy(ScpCfDestinationRetrievalStrategy.ALWAYS_SUBSCRIBER))
.build();
Try<Iterable<ScpCfDestination>> destinations = loader.tryGetAllDestinations(options);
Similar to the default behavior of DestinationAccessor API, in the code above only the subscriber account will be considered. Other options are:
ScpCfDestinationRetrievalStrategy.ALWAYS_SUBSCRIBER
ScpCfDestinationRetrievalStrategy.ALWAYS_PROVIDER
ScpCfDestinationRetrievalStrategy.SUBSCRIBER_THEN_PROVIDER

Generate openApi DTO's with NestJS without a Controller

I am writing a NestJS service that provides a REST API and it publishes some messages to NATS. We are using the NestJS support to generate OpenAPI docs, and from the OpenAPI docs we generate an SDK that we import into our clients. This all works great, but only the REST API of our code is in the SDK.
What we'd like to also do is to have NestJS include the DTO's for the content for the messages we publish to NATS. Then our SDK will also include interfaces for these DTO's, and then our clients can cast the message content to the correct interface (based on the message subject). This way, the publisher of an event defines the content of the event, and users of it don't have to replicate the interface, yet they get strongly-typed code.
I've tried adding the #Api decorators to the DTO, but it appears that unless the DTO is used in the definition of an #Controller, it is not included in the resultant openApi docs.
I was hoping for a way to decorate a "random" DTO in my code so it will then be included in the swagger docs, and in-turn included in a generated SDK. Is something like that possible?
you can also pass extraModels array as a part of SwaggerDocumentOptions
SwaggerModule.createDocument(app, config, {
extraModels: [.......]
});
https://github.com/nestjs/swagger/issues/241

Spring integration payload creation from header in XML config

I am looking for an Spring integration XML config that creates the payload object in a chain using one of the headers.
Java DSL has org.springframework.messaging.support.MessageBuilder.withPayload support that can create a message with payload, Can someone please share an XML configuration equivalent to this, which can set payload with a given header of the Message.
-Praveen
Clearly that is a Transformer responsibility and in pure xml you can do this:
<transformer expression ="headers.my_header_for_payload"/>
And the result of that SpEL becomes as a payload of output message from the transformer.

Foxx/ArangoDB: Can you create a response that adhere to JSON API specification?

I am currently writing some micro services with Foxx to be consumed by Ember.js. Ember data plays very nicely with JSON API (http://jsonapi.org) responses. So I tried to serialize the Foxx responses with the json-api-serializer (https://www.npmjs.com/package/json-api-serializer) - but with no luck. I only found the forClient method, but this only allows me to operate on the JSON representation of single objects, not the whole response. So my question: Is it possible to implement JSON API with Foxx/ArangoDB?
You can return arbitrary responses from Foxx routes, so it's entirely possible to generate JSON responses that conform to JSON API.
However there's no built-in way to do this automatically.
I don't see anything in json-api-serializer that shouldn't work in Foxx, so I'm not sure what problems you are encountering. You should be able to simply return the output object with res.json(outputFromSerializer) and set the content type with res.set('content-type', 'application/vnd.api+json').
If everything else fails you can just write your own helper functions to generate the boilerplate and metadata JSON API expects.

Mule Schema validation when wsdl has embedded xsd

In Mule ESB I want to validate incoming SOAP/XML, using a standard Mule "schema-validation filter".
Something like:
<mulexml:schema-validation-filter schemaLocations="xxx.xsd" name="xxxValidationFilter"/>
However this kind of definition seem to assume that the schema is located in a separate xsd-file, whereas in a lot of cases the schema definition is embedded in the "wsdl:types" element of a wsdl-file.
Is there any way to use the Mule schema-validation-filter to validate against schema's which are embedded in the wsdl (except for copying the schema element definitions out of the
wsdl and into a separate xsd-file).
Mule service element tags which expose the service haave an attribute that can enable validation on the incoming request.
validationEnabled="true"
<cxf:simple-service validationEnabled="true"/>
<cxf:jaxws-service validationEnabled="true"/>
<cxf:proxy-service validationEnabled="true"/>
For more information refer to the following Mule documentation link.
http://www.mulesoft.org/documentation/display/current/Building+Web+Services+with+CXF
Hope this helps.

Resources