====== Rest Jax-RS ======
* http://www.modio.io/developing-a-rest-service-in-apache-karaf-part-1/
* http://deemson.blogspot.de/2013/09/restful-web-services-in-osgi-container.html
* good: http://liquid-reality.de:8090/display/liquid/2011/12/22/Karaf+Tutorial+Part+4+-+CXF+Services+in+OSGi
* good: https://dzone.com/articles/building-cxf-rest-service-osgi
* https://help.talend.com/reader/3o89P0YRxO5wQCS2w857gA/HYdhz22ToUebKzabJ1EHbQ
====== Sample ======
* https://jar-download.com/explore-java-source-code-detail.php?file=./javaSource/org.apache.unomi/cxs-privacy-extension-rest/1.0.0-incubating/OSGI-INF/blueprint/blueprint.xml&key=3282c841fc898bae4d555fc656234757
* http://cxf.apache.org/docs/jax-rs-data-bindings.html
* https://stackoverflow.com/questions/20748079/jax-rs-returning-concrete-class-instance-from-method-with-declared-abstract-ret
* Complete JSON:
* http://www.benchresources.net/apache-cxf-jax-rs-restful-web-service-using-jaxb-json-example/
* https://examples.javacodegeeks.com/enterprise-java/rest/creating-jax-rs-web-service-using-apache-cxf/
* Webclient:
* http://www.kswaughs.com/2015/11/cxf-rest-client-to-call-post-method.html
====== Basic ======
===== JAXB - JAX-RS =====
* Book: RESTful Java with JAX-RS 2.0: Designing and Developing Distributed
* https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/index.html
* https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html/developing_web_services_applications/developing_jax_rs_web_services#default_providers_and_default_jax_rs_content_marshalling
==== JAX-RS Concepts ====
Entity Providers
These providers control the mapping of data representations (like XML, JSON, CSV) to their Java object equivalents.
Context Providers
These providers control the context that resources can access via @Context annotations.
Exception Providers
These providers control the mapping of Java exceptions to a JAX-RS Response instance.
public interface Providers {
MessageBodyReader getMessageBodyReader(Class type, Type genericType, Annotation annotations[], MediaType mediaType);
MessageBodyWriter getMessageBodyWriter(Class type, Type genericType, Annotation annotations[], MediaType mediaType);
ExceptionMapper getExceptionMapper(Class type);
ContextResolver getContextResolver(Class contextType, MediaType mediaType);
}
@Path("/something/")
class MyResource {
@Context
javax.ws.rs.ext.Providers providers;
@GET
public Response get() {
ContextResolver resolver = providers.getContextResolver(StorageEngine.class, MediaType.WILDCARD_TYPE);
StorageEngine engine = resolver.get(StorageEngine.class);
...
}
}
===== JAX-RS Request Processing =====
https://www.packtpub.com/graphics/9781784399092/graphics/B04017_04_01.jpg
{{ :programming:karaf:b04017_04_01.jpg?400 |}}
===== JSON Support =====
* Maybe old: http://cxf.apache.org/docs/json-support.html
* http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-ConfiguringJSONprovider
====== - Polymorphic resolution without annotations ======
* https://groups.google.com/forum/#!topic/jackson-user/3RalYqhYkRg
* https://stackoverflow.com/questions/10329706/json-deserialization-into-another-class-hierarchy-using-jackson
* http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
* http://wiki.fasterxml.com/JacksonMixInAnnotations
===== - Configure Jackson object mapper in CXF Provider =====
* Use-case: https://stackoverflow.com/questions/13503742/how-to-use-multiple-json-providers-with-cxf-spring-and-jackson
* https://dzone.com/articles/pragmatic-web-services-apache
* https://www.wolfe.id.au/2011/05/22/using-jackson-with-apache-cxf/
* https://stackoverflow.com/questions/14631972/using-dynamic-jackson-mix-ins-with-apache-cxf?rq=1
===== - Custom Provider: Serialization / Deserialization Message body reader writer =====
* http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-CustomMessageBodyProviders
* https://stackoverflow.com/questions/6312030/cxf-no-message-body-writer-found-for-class-automatically-mapping-non-simple-r
* example:
* custom jackson module: http://jmchung.github.io/blog/2014/06/18/how-to-customise-the-jackson-json-objectmapper-in-java-ee-enterprise-application/
* custom serializer, registation with objectMapper: http://www.baeldung.com/jackson-object-mapper-tutorial
* https://dzone.com/articles/custom-json-deserialization-with-jackson
* nested deserialization: https://stackoverflow.com/questions/43911622/nested-custom-deserialization-in-java-with-jackson
* http://www.baeldung.com/jackson-collection-array
* custome provider, registration:
* https://www.javatips.net/blog/apache-cxf-with-jackson?page=2
* https://stackoverflow.com/questions/6312030/cxf-no-message-body-writer-found-for-class-automatically-mapping-non-simple-r
* http://it-was-pain.blogspot.de/2012/05/apache-cxf-jackson.html
* https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=9798228#JAX-RS-MessageBodyProviders
* Code example:
* https://www.ibm.com/developerworks/library/wa-jaxrs/index.html
* http://book2s.com/java/src/package/com/netflix/explorers/providers/jsonmessagebodywriter.html
* https://stackoverflow.com/questions/18154983/how-do-i-validate-incoming-json-data-inside-a-rest-service
==== - Jersey vs CXF Custom Provider ====
* http://aleung.github.io/blog/2010/02/22/JSON-support-in-JAX-RS/
* https://tutel.me/c/programming/questions/28568130/implementing+hypermedia+in+restful+jaxrs+apache+cxf
==== - CXF vs Jersey and Jackson - Explaination ====
* http://ev9d9.blogspot.de/2013/05/doing-json-on-cxf.html
===== - ObjectMapperResolver =====
* https://gist.github.com/sprelacart/9758892
* http://jmchung.github.io/blog/2014/06/18/how-to-customise-the-jackson-json-objectmapper-in-java-ee-enterprise-application/
* https://blog.dejavu.sk/2014/02/11/inject-custom-java-types-via-jax-rs-parameter-annotations/
* https://blog.dejavu.sk/2015/03/24/what-to-do-when-jax-rs-cannot-find-its-providers-aka-my-message-body-writer-is-not-used/
===== - Custom ObjectMapper for Provider =====
==== - Add Jackson maven dependency ====
org.codehaus.jackson
jackson-jaxrs
1.9.7
==== - Include Jackson provider bean into your service providers: ====
Now all JSON responces from "clientService" are generated by Jackson.
==== - Jackson configuration ====
Enable wrapping entities with root element.
Exclude null values from JSON responce.
This features are disabled by default. To enable it I created my custom ObjectMapper class, that extends from "org.codehaus.jackson.map.ObjectMapper"
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
public class CustomObjectMapper extends ObjectMapper{
public CustomObjectMapper() {
super();
super.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
super.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
}
}
==== - Then added CustomObjectMapper to "jsonProvider" constructor: ====
===== - Fasterxml Polymorphic Type Handling =====
* JAX-RS:
* https://abhirockzz.wordpress.com/2016/01/20/using-parameter-converters-in-jax-rs/
* http://www.hameister.org/JEE7_JAXRS2_MesssageBodyReaderWriter.html
* https://cassiomolin.com/2017/11/21/customizing-objectmapper-in-a-jaxrs-application/
* http://www.baeldung.com/jackson-annotations
* https://stackoverflow.com/questions/5826928/how-can-i-prevent-jackson-from-serializing-a-polymorphic-types-annotation-prope
* **Uing mixin with a fresh ObjectMapper instance, globally declared mapper does not work**
* https://stackoverflow.com/questions/29388694/jackson-serialize-property-with-dynamically-different-names-using-mixins (last comment)
* http://programmerbruce.blogspot.de/2011/05/deserialize-json-with-jackson-into.html
==== - Custom Ser/Deser ====
* http://www.baeldung.com/jackson-serialize-field-custom-criteria
* Custom annotation: https://stackoverflow.com/questions/43342097/jackson-custom-annotation-for-custom-value-serialization
*
====== Jax-RS WebClient ======
* https://stackoverflow.com/questions/32601806/jaxbexception-occurred-unexpected-element-while-calling-rest-service-from-post
*
====== Troubleshooting ======
===== Proxy Webclient: service is not an interface =====
* http://cxf.547215.n5.nabble.com/WebClient-fails-to-unmarshall-td567073.html
Problem:
Rest service class is not interface so CGLIB proxy will be created by JAXRSClientFactory.
https://cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/client/JAXRSClientFactory.html
Solution:
Add this to class path
cglib
cglib-nodep
2.2.2
===== WebClient Error 400 JAXB expected.. =====
Error: JAXBException occurred : unexpected element (uri:"", local:"headers")
* https://stackoverflow.com/questions/32601806/jaxbexception-occurred-unexpected-element-while-calling-rest-service-from-post
* http://widequestion.com/question/how-to-define-model-to-unmarshall-post-data-without-root-element-name/
Cause:
xmlrootelement is serialized as wrapper element for json object. CXF JAXB always expects it but some clients only send the object.
Solution:
Ignoring the tags may not works correctly. As suggested: https://stackoverflow.com/a/16390260/707704 . The answers above give more information.
private static JacksonJsonProvider jackson_json_provider = new JacksonJaxbJsonProvider()
.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)