| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <!-- Google Tag Manager --> |
| <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| })(window,document,'script','dataLayer','GTM-MT3PBTF');</script> |
| <!-- End Google Tag Manager --> |
| |
| <style> |
| @import url("styles/juneau-code.css"); |
| @import url("styles/juneau-doc.css"); |
| </style> |
| </head> |
| <body> |
| <!-- Google Tag Manager (noscript) --> |
| <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MT3PBTF" |
| height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> |
| <!-- End Google Tag Manager (noscript) --> |
| |
| <!-- ======================================================================================================= --> |
| <!-- === JUNEAU-DTO ======================================================================================== --> |
| <!-- ======================================================================================================= --> |
| |
| <h5 class='toc' id='juneau-dto'>juneau-dto</h5> |
| <div> |
| <h5 class='figure'>Maven Dependency</h5> |
| <p class='bcode w500'> |
| <xt><dependency></xt> |
| <xt><groupId></xt>org.apache.juneau<xt></groupId></xt> |
| <xt><artifactId></xt>juneau-dto<xt></artifactId></xt> |
| <xt><version></xt>8.1.2<xt></version></xt> |
| <xt></dependency></xt> |
| </p> |
| |
| <h5 class='figure'>Java Library</h5> |
| <p class='bcode w500'> |
| juneau-dto-8.1.2.jar |
| </p> |
| |
| <h5 class='figure'>OSGi Module</h5> |
| <p class='bcode w500'> |
| org.apache.juneau.dto_8.1.2.jar |
| </p> |
| |
| <p> |
| Data Transfer Object libraries are provided for a variety of languages that allow you to serialize commonly-used |
| documents. |
| </p> |
| <ul> |
| <li>HTML5 |
| <li>Atom |
| <li>Cognos |
| <li>JSON-Schema |
| <li>Swagger 2.0 |
| </ul> |
| <p> |
| HTML5 documents and fragments can be constructed using the HTML5 DTOs and HTML or XML serializers: |
| </p> |
| <p class='bcode w800'> |
| <jk>import static</jk> org.apache.juneau.dto.html5.HtmlBuilder.*; |
| |
| Object myform = |
| <jsm>form</jsm>().action(<js>"/submit"</js>).method(<js>"POST"</js>) |
| .children( |
| <js>"Position (1-10000): "</js>, <jsm>input</jsm>(<js>"number"</js>).name(<js>"pos"</js>).value(1), <jsm>br</jsm>(), |
| <js>"Limit (1-10000): "</js>, <jsm>input</jsm>(<js>"number"</js>).name(<js>"limit"</js>).value(100), <jsm>br</jsm>(), |
| <jsm>button</jsm>(<js>"submit"</js>, <js>"Submit"</js>), |
| <jsm>button</jsm>(<js>"reset"</js>, <js>"Reset"</js>) |
| ); |
| |
| String html = HtmlSerializer.<jsf>DEFAULT</jsf>.serialize(myform); |
| </p> |
| <p class='bcode w800'><xt> |
| <form <xa>action</xa>=<xs>'/submit'</xs> <xa>method</xa>=<xs>'POST'</xs>> |
| <xv>Position (1-10000):</xv> <input <xa>name</xa>=<xs>'pos'</xs> <xa>type</xa>=<xs>'number'</xs> <xa>value</xa>=<xs>'1'</xs>/><br/> |
| <xv>Limit (1-10000):</xv> <input <xa>name</xa>=<xs>'pos'</xs> <xa>type</xa>=<xs>'number'</xs> <xa>value</xa>=<xs>'100'</xs>/><br/> |
| <button <xa>type</xa>=<xs>'submit'</xs>><xv>Submit</xv></button> |
| <button <xa>type</xa>=<xs>'reset'</xs>><xv>Reset</xv></button> |
| </form> |
| </xt></p> |
| <p> |
| And you're not limited to just HTML. The HTML5 beans are POJOs that can be serialized using any |
| of the serializers, such as lax JSON: |
| </p> |
| <p class='bcode w800'> |
| { |
| <jok>_type</jok>: <jov>'form'</jov>, |
| <jok>a</jok>: { <jok>action</jok>: <jov>'/submit'</jov>, <jok>method</jok>: <jov>'POST'</jov> }, |
| <jok>c</jok>: [ |
| <jov>'Position (1-10000): '</jov>, |
| { <jok>_type</jok>: <jov>'input'</jov>, <jok>a</jok>: { <jok>type</jok>: <jov>'number'</jov>, <jok>name</jok>: <jov>'pos'</jov>, <jok>value</jok>: <jov>1</jov> } }, |
| { <jok>_type</jok>: <jov>'br'</jov> }, |
| <jov>'Limit (1-10000): '</jov>, |
| { <jok>_type</jok>: <jov>'input'</jov>, <jok>a</jok>: { <jok>type</jok>: <jov>'number'</jov>, <jok>name</jok>: <jov>'limit'</jov>, <jok>value</jok>: <jov>100</jov> } }, |
| { <jok>_type</jok>: <jov>'br'</jov> }, |
| { <jok>_type</jok>: <jov>'button'</jov>, <jok>a</jok>: { <jok>type</jok>: <jov>'submit'</jov> }, <jok>c</jok>: [ <jov>'Submit'</jov> ] }, |
| { <jok>_type</jok>: <jov>'button'</jov>, <jok>a</jok>: { <jok>type</jok>: <jov>'reset'</jov> }, <jok>c</jok>: [ <jov>'Reset'</jov> ] } |
| ] |
| } |
| </p> |
| |
| <p> |
| ATOM feeds can be constructed using the ATOM DTOs and XML serializer: |
| </p> |
| <p class='bcode w800'> |
| <jk>import static</jk> org.apache.juneau.dto.atom.AtomBuilder.*; |
| |
| Feed feed = |
| <jsm>feed</jsm>(<js>"tag:juneau.apache.org"</js>, <js>"Juneau ATOM specification"</js>, <js>"2016-01-02T03:04:05Z"</js>) |
| .subtitle(<jsm>text</jsm>(<js>"html"</js>).text(<js>"Describes <em>stuff</em> about Juneau"</js>)) |
| .links( |
| <jsm>link</jsm>(<js>"alternate"</js>, <js>"text/html"</js>, <js>"http://juneau.apache.org/"</js>).hreflang(<js>"en"</js>), |
| <jsm>link</jsm>(<js>"self"</js>, <js>"application/atom+xml"</js>, <js>"http://juneau.apache.org/feed.atom"</js>) |
| ) |
| .rights(<js>"Copyright (c) 2016, Apache Foundation"</js>) |
| .entries( |
| <jsm>entry</jsm>(<js>"tag:juneau.sample.com,2013:1.2345"</js>, <js>"Juneau ATOM specification snapshot"</js>, <js>"2016-01-02T03:04:05Z"</js>) |
| .published(<js>"2016-01-02T03:04:05Z"</js>) |
| .content( |
| <jsm>content</jsm>(<js>"xhtml"</js>) |
| .lang(<js>"en"</js>) |
| .base(<js>"http://www.apache.org/"</js>) |
| .text(<js>"<div><p><i>[Update: Juneau supports ATOM.]</i></p></div>"</js>) |
| ) |
| ); |
| |
| <jc>// Serialize to ATOM/XML</jc> |
| String atomXml = XmlSerializer.<jsf>DEFAULT</jsf>.serialize(feed); |
| </p> |
| <p class='bcode w800'> |
| <xt><feed></xt> |
| <xt><id></xt> |
| tag:juneau.apache.org |
| <xt></id></xt> |
| <xt><link</xt> <xa>href</xa>=<xs>'http://juneau.apache.org/'</xs> <xa>rel</xa>=<xs>'alternate'</xs> <xa>type</xa>=<xs>'text/html'</xs> <xa>hreflang</xa>=<xs>'en'</xs>/<xt>></xt> |
| <xt><link</xt> <xa>href</xa>=<xs>'http://juneau.apache.org/feed.atom'</xs> <xa>rel</xa>=<xs>'self'</xs> <xa>type</xa>=<xs>'application/atom+xml'</xs>/<xt>></xt> |
| <xt><rights></xt> |
| Copyright (c) 2016, Apache Foundation |
| <xt></rights></xt> |
| <xt><title</xt> <xa>type</xa>=<xs>'text'</xs>></xt> |
| Juneau ATOM specification |
| <xt></title></xt> |
| <xt><updated></xt>2016-01-02T03:04:05Z<xt></updated></xt> |
| <xt><subtitle</xt> <xa>type</xa>=<xs>'html'</xs><xt>></xt> |
| Describes <em>stuff</em> about Juneau |
| <xt></subtitle></xt> |
| <xt><entry></xt> |
| <xt><id></xt> |
| tag:juneau.apache.org |
| <xt></id></xt> |
| <xt><title></xt> |
| Juneau ATOM specification snapshot |
| <xt></title></xt> |
| <xt><updated></xt>2016-01-02T03:04:05Z<xt></updated></xt> |
| <xt><content</xt> <xa>base</xa>=<xs>'http://www.apache.org/'</xs> <xa>lang</xa>=<xs>'en'</xs> <xa>type</xa>=<xs>'xhtml'</xs><xt>></xt> |
| <xt><div</xt> <xa>xmlns</xa>=<xs>"http://www.w3.org/1999/xhtml"</xs><xt>><p><i></xt>[Update: Juneau supports ATOM.]<xt></i></p></div></xt> |
| <xt></content></xt> |
| <xt><published></xt>2016-01-02T03:04:05Z<xt></published></xt> |
| <xt></entry></xt> |
| <xt></feed></xt> |
| </p> |
| <p> |
| Swagger documents can be constructed using the Swagger DTOs and JSON serializer: |
| </p> |
| <p class='bcode w800'> |
| <jk>import static</jk> org.apache.juneau.dto.swagger.SwaggerBuilder.*; |
| |
| Swagger swagger = <jsm>swagger</jsm>() |
| .swagger(<js>"2.0"</js>) |
| .info( |
| <jsm>info</jsm>(<js>"Swagger Petstore"</js>, <js>"1.0.0"</js>) |
| .description(<js>"This is a sample server Petstore server."</js>) |
| .termsOfService(<js>"http://swagger.io/terms/"</js>) |
| .contact( |
| <jsm>contact</jsm>().email(<js>"apiteam@swagger.io"</js>) |
| ) |
| .license( |
| <jsm>license</jsm>(<js>"Apache 2.0"</js>).url(<js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>) |
| ) |
| ) |
| .path(<js>"/pet"</js>, <js>"post"</js>, |
| <jsm>operation</jsm>() |
| .tags(<js>"pet"</js>) |
| .summary(<js>"Add a new pet to the store"</js>) |
| .description(<js>""</js>) |
| .operationId(<js>"addPet"</js>) |
| .consumes(MediaType.<jsf>JSON</jsf>, MediaType.<jsf>XML</jsf>) |
| .produces(MediaType.<jsf>JSON</jsf>, MediaType.<jsf>XML</jsf>) |
| .parameters( |
| <jsm>parameterInfo</jsm>(<js>"body"</js>, <js>"body"</js>) |
| .description(<js>"Pet object that needs to be added to the store"</js>) |
| .required(<jk>true</jk>) |
| ) |
| .response(405, <jsm>responseInfo</jsm>(<js>"Invalid input"</js>)) |
| ); |
| |
| <jc>// Serialize to Swagger/JSON</jc> |
| String swaggerJson = JsonSerializer.<jsf>DEFAULT_READABLE</jsf>.serialize(swagger); |
| </p> |
| <p class='bcode w800'> |
| { |
| <jok>"swagger"</jok>: <jov>"2.0"</jov>, |
| <jok>"info"</jok>: { |
| <jok>"title"</jok>: <jov>"Swagger Petstore"</jov>, |
| <jok>"description"</jok>: <jov>"This is a sample server Petstore server."</jov>, |
| <jok>"version"</jok>: <jov>"1.0.0"</jov>, |
| <jok>"termsOfService"</jok>: <jov>"http://swagger.io/terms/"</jov>, |
| <jok>"contact"</jok>: { |
| <jok>"email"</jok>: <jov>"apiteam@swagger.io"</jov> |
| }, |
| <jok>"license"</jok>: { |
| <jok>"name"</jok>: <jov>"Apache 2.0"</jov>, |
| <jok>"url"</jok>: <jov>"http://www.apache.org/licenses/LICENSE-2.0.html"</jov> |
| } |
| }, |
| <jok>"paths"</jok>: { |
| <jok>"/pet"</jok>: { |
| <jok>"post"</jok>: { |
| <jok>"tags"</jok>: [ |
| <jov>"pet"</jov> |
| ], |
| <jok>"summary"</jok>: <jov>"Add a new pet to the store"</jov>, |
| <jok>"description"</jok>: <jov>""</jov>, |
| <jok>"operationId"</jok>: <jov>"addPet"</jov>, |
| <jok>"consumes"</jok>: [ |
| <jov>"application/json"</jov>, |
| <jov>"text/xml"</jov> |
| ], |
| <jok>"produces"</jok>: [ |
| <jov>"application/json"</jov>, |
| <jov>"text/xml"</jov> |
| ], |
| <jok>"parameters"</jok>: [ |
| { |
| <jok>"in"</jok>: <jov>"body"</jov>, |
| <jok>"name"</jok>: <jov>"body"</jov>, |
| <jok>"description"</jok>: <jov>"Pet object that needs to be added to the store"</jov>, |
| <jok>"required"</jok>: <jov>true</jov> |
| } |
| ], |
| <jok>"responses"</jok>: { |
| <jok>"405"</jok>: { |
| <jok>"description"</jok>: <jov>"Invalid input"</jov> |
| } |
| } |
| } |
| } |
| } |
| } |
| </p> |
| <p> |
| Note that these DTOs can also be serialized to any of the other supported languages such as JSON or MessagePack! |
| And they can be parsed back into their original objects! |
| </p> |
| <p> |
| As a convenience, you can also simply call <code>toString()</code> on any of these DTOs and they will |
| be serialized directly to a string in the typical language (e.g. HTML5 beans to HTML, Swagger to JSON, etc...). |
| </p> |
| |
| <h5 class='section'>More Information:</h5> |
| <ul class='doctree'> |
| <li><a class='doclink' href='http://juneau.apache.org/site/apidocs-8.1.2/overview-summary.html#juneau-dto'>juneau-dto</a> |
| </ul> |
| </div> |
| </body> |
| </html> |