|
| 1 | +[//]: # " Copyright (c) 2015, 2020, 2026 Oracle and/or its affiliates. All rights reserved. " |
| 2 | +[//]: # " " |
| 3 | +[//]: # " This program and the accompanying materials are made available under the " |
| 4 | +[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at " |
| 5 | +[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. " |
| 6 | +[//]: # " " |
| 7 | +[//]: # " SPDX-License-Identifier: BSD-3-Clause " |
| 8 | + |
| 9 | +Jackson JAX-RS JSON Provider Example |
| 10 | +==================================== |
| 11 | + |
| 12 | +This example demonstrates how to produce/consume JSON representations |
| 13 | +from Java objects. This applies not only to JAXB beans, as shown in the |
| 14 | +`json-from-jaxb` example but also to ordinary, un-annotated, POJOs. |
| 15 | + |
| 16 | +This example hosts three simple read-only resources: One provides an |
| 17 | +example of using a Jackson JSON provider (registered by the feature |
| 18 | +`JacksonFeature` in the `MyApplication` class) instead of using JAXB |
| 19 | +(Object->JAXB->JSON) which has some limitations (e.g. empty arrays |
| 20 | +in JAXB beans). For this resource the JSON representation is produced by |
| 21 | +the Jackson JAX-RS provider, while the XML representation is generated |
| 22 | +by JAXB as usual. The second web resource is based on a simple |
| 23 | +un-annotated POJO and provides only JSON-based representations: JSON and |
| 24 | +JSON with padding (JSONP). The third resource depicts how Jackson and |
| 25 | +JAXB annotations could be mixed together within a single POJO. |
| 26 | + |
| 27 | +Contents |
| 28 | +-------- |
| 29 | + |
| 30 | +The "empty array" web resource is implemented by the `org.glassfish.jersey.examples.jackson3.EmptyArrayResource` class. |
| 31 | + |
| 32 | +The "non JAXB" web resource is implemented by the `org.glassfish.jersey.examples.jackson3.NonJaxbBeanResource` class. |
| 33 | + |
| 34 | +Both resources use the default Jackson mapper configuration to serialize JSON |
| 35 | +data out and depicts the corner cases, where the Jersey internal, StAX |
| 36 | +based, JSON processing can not be utilized. |
| 37 | + |
| 38 | +The `org.glassfish.jersey.examples.jackson3.CombinedAnnotationResource` |
| 39 | +class is used to show how JAXB and Jackson annotations could be combined |
| 40 | +together in one Java bean for JSON serialization. See the |
| 41 | + |
| 42 | +`org.glassfish.jersey.examples.jackson3.MyJsonMapperProvider` class |
| 43 | +where Jackson specific options are used to set up the desired JSON |
| 44 | +serialization configuration. |
| 45 | + |
| 46 | +The mapping of the URI path space is presented in the following table: |
| 47 | + |
| 48 | +URI path | Resource class | HTTP method |
| 49 | +---------------------------- | ---------------------------- | ------------- |
| 50 | +**_/emptyArrayResource_** | EmptyArrayResource | GET |
| 51 | +**_/nonJaxbResource_** | NonJaxbBeanResource | GET |
| 52 | +**_/combinedAnnotations_** | CombinedAnnotationResource | GET |
| 53 | + |
| 54 | +To use Jackson specific configuration options, one can implement a |
| 55 | +`ContextResolver<JsonMapper>` provider. For an example of such an |
| 56 | +implementation, see the `MyJsonMapperProvider` class. |
| 57 | + |
| 58 | +Running the Example |
| 59 | +------------------- |
| 60 | + |
| 61 | +Run the example as follows: |
| 62 | + |
| 63 | +> mvn clean compile exec:java |
| 64 | +
|
| 65 | +This deploys the example using[Grizzly](https://projects.eclipse.org/projects/ee4j.grizzly) |
| 66 | + |
| 67 | +A [WADL description](http://wadl.java.net/#spec) may be accessed at the URL: |
| 68 | + |
| 69 | +- <http://localhost:8080/jackson/application.wadl> |
| 70 | + |
| 71 | +The three resources are available at |
| 72 | + |
| 73 | +- <http://localhost:8080/jackson/emptyArrayResource> |
| 74 | +- <http://localhost:8080/jackson/nonJaxbResource> |
| 75 | +- <http://localhost:8080/jackson/combinedAnnotations> |
| 76 | + |
| 77 | +To easily obtain the different output types available, |
| 78 | +[cURL](http://curl.haxx.se/) can be used as follows: |
| 79 | + |
| 80 | +Obtain the JSON output of EmptyArrayResource (use |
| 81 | +`-HAccept:application/xml` for XML output): |
| 82 | + |
| 83 | +> curl -HAccept:application/json http://localhost:8080/jackson/emptyArrayResource |
| 84 | +
|
| 85 | +Obtain the JSON output of NonJaxbBeanResource (use `-HAccept:application/javascript` for JSONP output): |
| 86 | + |
| 87 | +> curl -HAccept:application/json http://localhost:8080/jackson/nonJaxbResource |
| 88 | +
|
| 89 | +Obtain the JSON output of CombinedAnnotationResource: |
| 90 | + |
| 91 | +> curl -HAccept:application/json http://localhost:8080/jackson/combinedAnnotations |
0 commit comments