Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.72 KB | None | 0 0
  1. -----------------------------------com.example.Example.java-----------------------------------
  2.  
  3. package com.example;
  4.  
  5. import java.util.List;
  6. import com.google.gson.annotations.Expose;
  7. import com.google.gson.annotations.SerializedName;
  8.  
  9. public class Example {
  10.  
  11. @SerializedName("status")
  12. @Expose
  13. private String status;
  14. @SerializedName("copyright")
  15. @Expose
  16. private String copyright;
  17. @SerializedName("section")
  18. @Expose
  19. private String section;
  20. @SerializedName("last_updated")
  21. @Expose
  22. private String lastUpdated;
  23. @SerializedName("num_results")
  24. @Expose
  25. private Integer numResults;
  26. @SerializedName("results")
  27. @Expose
  28. private List<Result> results = null;
  29.  
  30. public String getStatus() {
  31. return status;
  32. }
  33.  
  34. public void setStatus(String status) {
  35. this.status = status;
  36. }
  37.  
  38. public String getCopyright() {
  39. return copyright;
  40. }
  41.  
  42. public void setCopyright(String copyright) {
  43. this.copyright = copyright;
  44. }
  45.  
  46. public String getSection() {
  47. return section;
  48. }
  49.  
  50. public void setSection(String section) {
  51. this.section = section;
  52. }
  53.  
  54. public String getLastUpdated() {
  55. return lastUpdated;
  56. }
  57.  
  58. public void setLastUpdated(String lastUpdated) {
  59. this.lastUpdated = lastUpdated;
  60. }
  61.  
  62. public Integer getNumResults() {
  63. return numResults;
  64. }
  65.  
  66. public void setNumResults(Integer numResults) {
  67. this.numResults = numResults;
  68. }
  69.  
  70. public List<Result> getResults() {
  71. return results;
  72. }
  73.  
  74. public void setResults(List<Result> results) {
  75. this.results = results;
  76. }
  77.  
  78. }
  79. -----------------------------------com.example.Multimedium.java-----------------------------------
  80.  
  81. package com.example;
  82.  
  83. import com.google.gson.annotations.Expose;
  84. import com.google.gson.annotations.SerializedName;
  85.  
  86. public class Multimedium {
  87.  
  88. @SerializedName("url")
  89. @Expose
  90. private String url;
  91. @SerializedName("format")
  92. @Expose
  93. private String format;
  94. @SerializedName("height")
  95. @Expose
  96. private Integer height;
  97. @SerializedName("width")
  98. @Expose
  99. private Integer width;
  100. @SerializedName("type")
  101. @Expose
  102. private String type;
  103. @SerializedName("subtype")
  104. @Expose
  105. private String subtype;
  106. @SerializedName("caption")
  107. @Expose
  108. private String caption;
  109. @SerializedName("copyright")
  110. @Expose
  111. private String copyright;
  112.  
  113. public String getUrl() {
  114. return url;
  115. }
  116.  
  117. public void setUrl(String url) {
  118. this.url = url;
  119. }
  120.  
  121. public String getFormat() {
  122. return format;
  123. }
  124.  
  125. public void setFormat(String format) {
  126. this.format = format;
  127. }
  128.  
  129. public Integer getHeight() {
  130. return height;
  131. }
  132.  
  133. public void setHeight(Integer height) {
  134. this.height = height;
  135. }
  136.  
  137. public Integer getWidth() {
  138. return width;
  139. }
  140.  
  141. public void setWidth(Integer width) {
  142. this.width = width;
  143. }
  144.  
  145. public String getType() {
  146. return type;
  147. }
  148.  
  149. public void setType(String type) {
  150. this.type = type;
  151. }
  152.  
  153. public String getSubtype() {
  154. return subtype;
  155. }
  156.  
  157. public void setSubtype(String subtype) {
  158. this.subtype = subtype;
  159. }
  160.  
  161. public String getCaption() {
  162. return caption;
  163. }
  164.  
  165. public void setCaption(String caption) {
  166. this.caption = caption;
  167. }
  168.  
  169. public String getCopyright() {
  170. return copyright;
  171. }
  172.  
  173. public void setCopyright(String copyright) {
  174. this.copyright = copyright;
  175. }
  176.  
  177. }
  178. -----------------------------------com.example.Result.java-----------------------------------
  179.  
  180. package com.example;
  181.  
  182. import java.util.List;
  183. import com.google.gson.annotations.Expose;
  184. import com.google.gson.annotations.SerializedName;
  185.  
  186. public class Result {
  187.  
  188. @SerializedName("section")
  189. @Expose
  190. private String section;
  191. @SerializedName("subsection")
  192. @Expose
  193. private String subsection;
  194. @SerializedName("title")
  195. @Expose
  196. private String title;
  197. @SerializedName("abstract")
  198. @Expose
  199. private String _abstract;
  200. @SerializedName("url")
  201. @Expose
  202. private String url;
  203. @SerializedName("byline")
  204. @Expose
  205. private String byline;
  206. @SerializedName("item_type")
  207. @Expose
  208. private String itemType;
  209. @SerializedName("updated_date")
  210. @Expose
  211. private String updatedDate;
  212. @SerializedName("created_date")
  213. @Expose
  214. private String createdDate;
  215. @SerializedName("published_date")
  216. @Expose
  217. private String publishedDate;
  218. @SerializedName("material_type_facet")
  219. @Expose
  220. private String materialTypeFacet;
  221. @SerializedName("kicker")
  222. @Expose
  223. private String kicker;
  224. @SerializedName("des_facet")
  225. @Expose
  226. private List<String> desFacet = null;
  227. @SerializedName("org_facet")
  228. @Expose
  229. private List<String> orgFacet = null;
  230. @SerializedName("per_facet")
  231. @Expose
  232. private List<String> perFacet = null;
  233. @SerializedName("geo_facet")
  234. @Expose
  235. private List<String> geoFacet = null;
  236. @SerializedName("multimedia")
  237. @Expose
  238. private List<Multimedium> multimedia = null;
  239. @SerializedName("short_url")
  240. @Expose
  241. private String shortUrl;
  242.  
  243. public String getSection() {
  244. return section;
  245. }
  246.  
  247. public void setSection(String section) {
  248. this.section = section;
  249. }
  250.  
  251. public String getSubsection() {
  252. return subsection;
  253. }
  254.  
  255. public void setSubsection(String subsection) {
  256. this.subsection = subsection;
  257. }
  258.  
  259. public String getTitle() {
  260. return title;
  261. }
  262.  
  263. public void setTitle(String title) {
  264. this.title = title;
  265. }
  266.  
  267. public String getAbstract() {
  268. return _abstract;
  269. }
  270.  
  271. public void setAbstract(String _abstract) {
  272. this._abstract = _abstract;
  273. }
  274.  
  275. public String getUrl() {
  276. return url;
  277. }
  278.  
  279. public void setUrl(String url) {
  280. this.url = url;
  281. }
  282.  
  283. public String getByline() {
  284. return byline;
  285. }
  286.  
  287. public void setByline(String byline) {
  288. this.byline = byline;
  289. }
  290.  
  291. public String getItemType() {
  292. return itemType;
  293. }
  294.  
  295. public void setItemType(String itemType) {
  296. this.itemType = itemType;
  297. }
  298.  
  299. public String getUpdatedDate() {
  300. return updatedDate;
  301. }
  302.  
  303. public void setUpdatedDate(String updatedDate) {
  304. this.updatedDate = updatedDate;
  305. }
  306.  
  307. public String getCreatedDate() {
  308. return createdDate;
  309. }
  310.  
  311. public void setCreatedDate(String createdDate) {
  312. this.createdDate = createdDate;
  313. }
  314.  
  315. public String getPublishedDate() {
  316. return publishedDate;
  317. }
  318.  
  319. public void setPublishedDate(String publishedDate) {
  320. this.publishedDate = publishedDate;
  321. }
  322.  
  323. public String getMaterialTypeFacet() {
  324. return materialTypeFacet;
  325. }
  326.  
  327. public void setMaterialTypeFacet(String materialTypeFacet) {
  328. this.materialTypeFacet = materialTypeFacet;
  329. }
  330.  
  331. public String getKicker() {
  332. return kicker;
  333. }
  334.  
  335. public void setKicker(String kicker) {
  336. this.kicker = kicker;
  337. }
  338.  
  339. public List<String> getDesFacet() {
  340. return desFacet;
  341. }
  342.  
  343. public void setDesFacet(List<String> desFacet) {
  344. this.desFacet = desFacet;
  345. }
  346.  
  347. public List<String> getOrgFacet() {
  348. return orgFacet;
  349. }
  350.  
  351. public void setOrgFacet(List<String> orgFacet) {
  352. this.orgFacet = orgFacet;
  353. }
  354.  
  355. public List<String> getPerFacet() {
  356. return perFacet;
  357. }
  358.  
  359. public void setPerFacet(List<String> perFacet) {
  360. this.perFacet = perFacet;
  361. }
  362.  
  363. public List<String> getGeoFacet() {
  364. return geoFacet;
  365. }
  366.  
  367. public void setGeoFacet(List<String> geoFacet) {
  368. this.geoFacet = geoFacet;
  369. }
  370.  
  371. public List<Multimedium> getMultimedia() {
  372. return multimedia;
  373. }
  374.  
  375. public void setMultimedia(List<Multimedium> multimedia) {
  376. this.multimedia = multimedia;
  377. }
  378.  
  379. public String getShortUrl() {
  380. return shortUrl;
  381. }
  382.  
  383. public void setShortUrl(String shortUrl) {
  384. this.shortUrl = shortUrl;
  385. }
  386.  
  387. }
  388. Use this tool offline:Maven pluginGradle pluginAnt taskCLIJava API
  389. Reference
  390. properties
  391. For each property present in the 'properties' definition, we add a property to a given Java class according to the JavaBeans spec. A private field is added to the parent class, along with accompanying accessor methods (getter and setter).
  392.  
  393. E.g. json schema
  394.  
  395. {
  396. "type" : "object",
  397. "properties" : {
  398. "foo" : {
  399. "type" : "string"
  400. }
  401. }
  402. }
  403. resulting Java type:
  404.  
  405. public class MyObject {
  406. private String foo;
  407. public String getFoo() {
  408. return foo;
  409. }
  410. public void setFoo(String foo) {
  411. this.foo = foo;
  412. }
  413. }
  414. If the generate-builders property is set to true, then a builder method is also added:
  415.  
  416. public MyObject withFoo(String foo) {
  417. this.foo = foo;
  418. return this;
  419. }
  420. These builder methods allow easy, one-liner construction and initialization of objects, like:
  421.  
  422. MyObject o = new MyObject().withFoo("foo").withBar("bar").withBaz("baz");
  423. type
  424. When encountering the type attribute (e.g. for properties), jsonschema2pojo maps schema types as follows:
  425.  
  426. Schema type Java type
  427. string java.lang.String
  428. number java.lang.Double
  429. integer java.lang.Integer
  430. boolean java.lang.Boolean
  431. object generated Java type
  432. array java.util.List
  433. array (with "uniqueItems":true) java.util.Set
  434. null java.lang.Object
  435. any java.lang.Object
  436. When applying the usePrimitives option, the primitives double, integer and boolean will replace the wrapper types listed above.
  437.  
  438. additionalProperties
  439. If additionalProperties is specified and set to the boolean value false, then the generated Java type does not support additional properties.
  440.  
  441. If the additionalProperties node is undefined (not present), null or empty, then a new bean property named "additionalProperties", of type Map<String,Object> is added to the generated type (with appropriate accessors). The accessors are annotated to allow Jackson to marshal/unmarshal unrecognised (additional) properties found in JSON data from/to this map.
  442.  
  443. So, schema file myObject.json like:
  444.  
  445. {
  446. "type" : "object"
  447. }
  448. or
  449.  
  450. {
  451. "type" : "object",
  452. "additionalProperties" : {}
  453. }
  454. produces:
  455.  
  456. public class MyObject {
  457.  
  458. private java.util.Map<String, Object> additionalProperties = new java.util.HashMap<String, Object>();
  459.  
  460. @org.codehaus.jackson.annotate.JsonAnyGetter
  461. public java.util.Map<String, Object> getAdditionalProperties() {
  462. return this.additionalProperties;
  463. }
  464.  
  465. @org.codehaus.jackson.annotate.JsonAnySetter
  466. public void setAdditionalProperties(String name, Object value) {
  467. this.additionalProperties.put(name, value);
  468. }
  469.  
  470. }
  471. If the additionalProperties node is present and specifies a schema, then an "additionalProperties" map is added to the generated type and map values will be restricted according to the additionalProperties schema.
  472.  
  473. So, schema file myObject.json like:
  474.  
  475. {
  476. "type" : "object",
  477. "additionalProperties" : {
  478. "type" : "number"
  479. }
  480. }
  481. produces:
  482.  
  483. public class MyObject {
  484.  
  485. private java.util.Map<String, Double> additionalProperties = new java.util.HashMap<String, Double>();
  486.  
  487. @org.codehaus.jackson.annotate.JsonAnyGetter
  488. public java.util.Map<String, Double> getAdditionalProperties() {
  489. return this.additionalProperties;
  490. }
  491.  
  492. @org.codehaus.jackson.annotate.JsonAnySetter
  493. public void setAdditionalProperties(String name, Double value) {
  494. this.additionalProperties.put(name, value);
  495. }
  496.  
  497. }
  498. Where the additionalProperties schema species a type object, map values will be restricted to instances of a newly generated Java type. If the given schema does not specify the =javaType= property, the name of the newly generated type will be derived from the parent type name and the suffix 'Property'.
  499.  
  500. So, schema file myObject.json like:
  501.  
  502. {
  503. "type" : "object",
  504. "additionalProperties" : {
  505. "type" : "object"
  506. }
  507. }
  508. produces:
  509.  
  510. public class MyObject {
  511.  
  512. private java.util.Map<String, MyObjectProperty> additionalProperties = new java.util.HashMap<String, MyObjectProperty>();
  513.  
  514. @org.codehaus.jackson.annotate.JsonAnyGetter
  515. public java.util.Map<String, MyObjectProperty> getAdditionalProperties() {
  516. return this.additionalProperties;
  517. }
  518.  
  519. @org.codehaus.jackson.annotate.JsonAnySetter
  520. public void setAdditionalProperties(String name, MyObjectProperty value) {
  521. this.additionalProperties.put(name, value);
  522. }
  523.  
  524. }
  525. items
  526. The 'items' rule defines a schema for the contents of an array. In generated Java types, the value of 'items' dictates the generic type of Lists and Sets.
  527.  
  528. So, this example JSON Schema:
  529.  
  530. {
  531. "type" : "object",
  532. "properties" : {
  533. "myArrayProperty" : {
  534. "type" : "array",
  535. "items" : {
  536. "type" : "string"
  537. }
  538. }
  539. }
  540. }
  541. produces a property called myArrayProperty of type List<String> in the generated Java type. If items itself declares a complex type ("type" : "object") then the generic type of the List or Set will itself be a generated type e.g. List<MyComplexType>.
  542.  
  543. required
  544. The 'required' schema rule doesn't produce a structural change in generated Java types, it simply causes the text (Required) to be added to the JavaDoc for fields, getters and setters.
  545.  
  546. optional
  547. The 'optional' schema rule doesn't produce a structural change in generated Java types, it simply causes the text (Optional) to be added to the JavaDoc for fields, getters and setters.
  548.  
  549. This schema rule is deprecated since Draft 03 of the JSON Schema specification. Rather than marking optional properties as optional, one should mark required properties as required.
  550.  
  551. uniqueItems
  552. For properties of type 'array', setting uniqueItems to false (or omitting it entirely) causes the generated Java property to be of type java.util.List.
  553.  
  554. When uniqueItems is set to true, the generated Java property value is of type java.util.Set.
  555.  
  556. enum
  557. When jsonschema2pojo encounters JSON Schema declarations of type "enum" it generates a Java enum type.
  558.  
  559. When a generated type includes a property of type "enum", the generated enum type becomes a static inner type declared within the enclosing (parent) generated type. If an enum is declared at the root of a schema, the generated enum is a public Java type with no enclosing type.
  560.  
  561. The actual enum value is held in a 'value' property inside the enum constants. The generated enum type also includes annotations that allow Jackson to correctly marshal/unmarshal JSON values, even when the actual values contain spaces, start with digits, or contain other characters that cannot legally form part of the Java enum constant name.
  562.  
  563. So, if we declare a schema myObject.json with an enum property:
  564.  
  565. {
  566. "type" : "object",
  567. "properties" : {
  568. "myEnum" : {
  569. "type" : "string",
  570. "enum" : ["one", "secondOne", "3rd one"]
  571. }
  572. }
  573. }
  574. we see a generated MyObject Java type with an inner enum type like:
  575.  
  576. @Generated("com.googlecode.jsonschema2pojo")
  577. public static enum MyEnum {
  578.  
  579. ONE("one"),
  580. SECOND_ONE("secondOne"),
  581. _3_RD_ONE("3rd one");
  582. private final String value;
  583.  
  584. private MyEnum(String value) {
  585. this.value = value;
  586. }
  587.  
  588. @JsonValue
  589. @Override
  590. public String toString() {
  591. return this.value;
  592. }
  593.  
  594. @JsonCreator
  595. public static MyObject.MyEnum fromValue(String value) {
  596. for (MyObject.MyEnum c: MyObject.MyEnum.values()) {
  597. if (c.value.equals(value)) {
  598. return c;
  599. }
  600. }
  601. throw new IllegalArgumentException(value);
  602. }
  603.  
  604. }
  605. default
  606. Using the default rule in your JSON Schema causes the corresponding property in your generated Java type to be initialised with a default value. You'll see the default value is assigned during field declaration.
  607.  
  608. Default values are supported for the JSON Schema properties of type string, integer, number and boolean; for enum properties; for properties with format of utc-millisec or date-time; for arrays of any of these types.
  609.  
  610. Some example JSON Schema property definitions and their corresponding Java field declaration are shown:
  611.  
  612. JSON Schema Java
  613. myString : { "type":"string", "default":"abc"} private String myString = "abc";
  614. myInteger : { "type":"integer", "default":"100"} private Integer myInteger = 100;
  615. myNumber : { "type":"number", "default":"10.3"} private Double myNumber = 10.3D;
  616. myMillis : { "type":"string", "format":"utc-millisec", "default":"500"} private Long myMillis = 500L;
  617. myDate : { "type":"string", "format":"date-time", "default":"500"} private Date myDate = new Date(500L);
  618. myDate : { "type":"string", "format":"date-time", "default":"2011-02-24T09:25:23.112+0000"} private Date myDate = new Date(1298539523112L);
  619. myList : { "type":"array", "default":["a","b","c"]} private List<String> myList = new ArrayList<String>(Arrays.asList("a","b","c"));
  620. As the above table shows, dates can be given a default using either a number of millis since epoch or a date string (ISO 8601 or RFC 1123). In either case, the date will be initialized using a millisecond value in the generated Java type.
  621.  
  622. title
  623. The 'title' rule appearing in a JSON Schema causes the given title text to be added to the JavaDoc for a property. The title text will appear in JavaDoc comments of the field, getter and setter.
  624.  
  625. title text always appears before description text.
  626.  
  627. description
  628. The description rule appearing in a JSON Schema causes the given description text to be added to the JavaDoc for a property. The description text will appear in JavaDoc comments of the field, getter and setter.
  629.  
  630. description text always appears after title text.
  631.  
  632. format
  633. Using the format rule for a property can influence the Java type chosen to represent your property values. When you use a format value that jsonschema2pojo recognises, it will use a better (more appropriate) type for your Java bean property.
  634.  
  635. For example, when I define a JSON property in my schema of type string, if I also attach the rule "format" : "uri" to that property (because my string is actually a URI) then my corresponding Java property will now have the type java.net.URI instead of java.lang.String.
  636.  
  637. How jsonschema2pojo maps format values to Java types:
  638.  
  639. Format value Java type
  640. "date-time" java.util.Date
  641. "date" String
  642. "time" String
  643. "utc-millisec" long
  644. "regex" java.util.regex.Pattern
  645. "color" String
  646. "style" String
  647. "phone" String
  648. "uri" java.net.URI
  649. "email" String
  650. "ip-address" String
  651. "ipv6" String
  652. "host-name" String
  653. "uuid" java.util.UUID
  654. anything else (unrecognised format) type is unchanged
  655. extends
  656. When the extends rule is encountered in your JSON schema (to indicate that one type extends another), this will produce an extends relationship in your generated Java types. That extends value can be a schema given in-line, or referenced using $ref.
  657.  
  658. As an example, lets imagine a file flower.json with the following content:
  659.  
  660. {
  661. "type" : "object"
  662. }
  663. and a second file rose.json which contains:
  664.  
  665. {
  666. "type" : "object",
  667. "extends" : {
  668. "$ref" : "flower.json"
  669. }
  670. }
  671. The two resulting Java types generated from these schemas would be:
  672.  
  673. public class Flower {
  674. ....
  675. }
  676. and
  677.  
  678. public class Rose extends Flower {
  679. ....
  680. }
  681. Note: the extends rule in JSON schema accepts a schema or an array of schemas - jsonschema2pojo only supports the single schema variant.
  682.  
  683. $ref
  684. A '$ref' rule can be used wherever a schema is expected i.e at the root of a schema document, as part of a property definition, as part of the items definition for an array type, as part of an additionalProperties definition.
  685.  
  686. Supported protocols:
  687.  
  688. http://, https://
  689. file://
  690. classpath:, resource:, java: (all synonyms used to resolve schemas from the classpath).
  691. Note: If you want to refer to classpath resources from the current Maven module, you will need to bind jsonschema2pojo to a later phase. By default jsonschema2pojo is bound to generate-sources but if you want resources present in the current module to be on the classpath when the plugin executes, then you should bind jsonschema2pojo to the process-resources phase.
  692.  
  693. For example, to refer to another document in the same directory which will provide the definition of a 'user' object, you might create a schema like:
  694.  
  695. {
  696. "type" : "object",
  697. "properties" : {
  698. "loggedInUser" : {
  699. "$ref" : "user.json"
  700. }
  701. }
  702. }
  703. jsonschema2pojo expects $ref values (URIs) to be URLs. Both absolute and relative URLs are valid. You may also refer to part of a schema document using the '#' character followed by a slash or dot delimited path.
  704.  
  705. Example using an absolute reference:
  706.  
  707. {
  708. "type" : "object",
  709. "properties" : {
  710. "address" : {
  711. "$ref" : "http://json-schema.org/address"
  712. }
  713. }
  714. }
  715. Example using a fragment path to reuse a schema definition:
  716.  
  717. {
  718. "type" : "object",
  719. "properties" : {
  720. "child1" : {
  721. "type" : "string"
  722. },
  723. "child2" : {
  724. "$ref" : "#/properties/child1"
  725. }
  726. }
  727. }
  728. Example treeNode.json using a self reference to build a tree:
  729.  
  730. {
  731. "description" : "Tree node",
  732. "type" : "object",
  733. "properties" : {
  734. "children" : {
  735. "type" : "array",
  736. "items" : {
  737. "$ref" : "#"
  738. }
  739. }
  740. }
  741. }
  742. which produces Java code similar to:
  743.  
  744. public class TreeNode {
  745.  
  746. public List<TreeNode> getChildren() {...}
  747.  
  748. public void setChildren(List<TreeNode> children) {...}
  749.  
  750. }
  751. minimum/maximum, minItems/maxItems, minLength/maxLength, required, pattern
  752. The Maven plugin, CLI and Ant task allow JSR-303 annotations to be activated via a config argument. When activated, the following JSR-303 annotations will be generated:
  753.  
  754. Schema rule Annotation
  755. maximum @DecimalMax
  756. minimum @DecimalMin
  757. minItems,maxItems @Size
  758. minLength,maxLength @Size
  759. pattern @Pattern
  760. required @NotNull
  761. Extensions
  762. javaType
  763. jsonschema2pojo supports an extension property 'javaType' that applies to schemas and allows you to specify a fully qualified name for your generated Java type.
  764.  
  765. For example, imagine a schema fooBar.json like:
  766.  
  767. {
  768. "type" : "object"
  769. }
  770. When invoking jsonschema2pojo with package argument com.example the generated Java type will have a fully qualified name of com.example.FooBar.
  771.  
  772. If the javaType property is added to fooBar.json like:
  773.  
  774. {
  775. "javaType" : "com.other.package.CustomTypeName",
  776. "type" : "object"
  777. }
  778. Then invoking jsonschema2pojo with package argument com.example will result in a generated Java type with a fully qualified name of com.other.package.CustomTypeName.
  779.  
  780. The javaType property may appear in any schema definition, not just the root schema in a schema document. For example, this file 'parent.json' invoked using package name com.example:
  781.  
  782. {
  783. "type" : "object",
  784. "properties" : {
  785. "myProperty" : {
  786. "javaType" : "com.other.package.CustomChildName",
  787. "type" : "object"
  788. }
  789. }
  790. }
  791. will result in two generated Java types:
  792.  
  793. com.example.Parent
  794. com.other.package.CustomChildName
  795. The javaType property also allows existing Java types to be used in your POJOs. If the value of javaType refers to an existing class or interface on the classpath, then that class/interface will be used (a new Java type will not be generated).
  796.  
  797. When referencing existing classes, it's also possible to supply generic type arguments, for instance:
  798.  
  799. {
  800. "type" : "object",
  801. "properties" : {
  802. "myProperty" : {
  803. "javaType" : "java.util.Map<String,Integer>",
  804. "type" : "object"
  805. }
  806. }
  807. }
  808. javaEnumNames
  809. Any schema that makes use of enum may include javaEnumNames. This property allows you to take control of naming your Java enum constants and avoid relying on auto-generated names.
  810.  
  811. If your schema includes javaEnumNames like:
  812.  
  813. {
  814. "type" : "object",
  815. "properties" : {
  816. "foo" : {
  817. "type" : "string",
  818. "enum" : ["H","L"],
  819. "javaEnumNames" : ["HIGH","LOW"]
  820. }
  821. }
  822. }
  823. Then you'll see a generated type like:
  824.  
  825. public enum Foo {
  826. HIGH("H"),
  827. LOW("L")
  828. ...
  829. }
  830. javaInterfaces
  831. Any schema may include a javaInterfaces property, the value of this property is an array of strings. Each string is expected to contain the fully-qualified name of a Java interface. The Java type generated by the schema will implement all the given interfaces.
  832.  
  833. If the javaInterfaces property is added to fooBar.json like:
  834.  
  835. {
  836. "javaInterfaces" : ["java.io.Serializable", "Cloneable"],
  837. "type" : "object"
  838. }
  839. then the result will be a class defined like:
  840.  
  841. public class FooBar implements Serializable, Cloneable
  842. {
  843. ...
  844. javaJsonView
  845. Any schema may include a javaJsonView property, the value of the property is a fully qualified class name. The property causes a @JsonView annotation to be added to the member with the given class for a value. This only works for Jackson-based annotators. It is supported for v1 and v2 Jackson. See Jackson Annotations.
  846.  
  847. If javaJsonView is added to fooBar.json schema as follows:
  848.  
  849. {
  850. "type": "object",
  851. "properties": {
  852. "internalStatus": {
  853. "javaJsonView": "com.example.views.Internal",
  854. "type": "string"
  855. }
  856. }
  857. }
  858. Then the resulting FooBar.java will have:
  859.  
  860. import com.example.views.Internal;
  861.  
  862. public class FooBar {
  863. @JsonView(Internal.class)
  864. private String internalStatus;
  865. }
  866. javaName
  867. Using javaName rule allows you to define custom names for java bean properties instead of those inferred from the corresponding json property names. This also affects setters and getters.
  868.  
  869. For example, the following schema:
  870.  
  871. {
  872. "type": "object",
  873. "properties": {
  874. "a": {
  875. "javaName": "b",
  876. "type": "string"
  877. }
  878. }
  879. }
  880. will produce Java code similar to the following:
  881.  
  882. public class MyClass {
  883. @JsonProperty("a")
  884. private String b;
  885.  
  886. @JsonProperty("a")
  887. public String getB() {
  888. return b;
  889. }
  890.  
  891. @JsonProperty("a")
  892. public void setB(String b) {
  893. this.b = b;
  894. }
  895. }
  896. customPattern/customTimezone
  897. Set a custom format pattern for date or date-time fields that will be used during serialization (if your binding library supports it). If you're using Jackson 2 this will cause an @JsonFormat annotation to be added. For 'date-time' fields you may also set a custom timezone (if you want to use a timezone other than UTC when writing out the date-time as a string).
  898.  
  899. {
  900. "type" : "object",
  901. "properties" : {
  902. "dob" : {
  903. "type" : "string",
  904. "format" : "date",
  905. "customPattern" : "yy-MM-dd"
  906. },
  907. "updatedDate" : {
  908. "type" : "string",
  909. "format" : "date-time",
  910. "customDateTimePattern" : "yyyy-MM-dd'T'HH:mm:ssZ",
  911. "customTimezone": "PDT"
  912. }
  913. }
  914. }
  915. There are two important things to note before using this extension property:
  916.  
  917. If you set the formatDates/formatDateTimes global config options to true, you'll get default ISO-8601/RFC 3339 formatting without having to set these custom properties.
  918. You should be aware that setting a custom date or date-time pattern may mean that the JSON content you produce is not valid according to the rules of JSON Schema.
  919. excludedFromEqualsAndHashCode
  920. You can specify which fields should be omitted in the generated equals() and hashCode() methods using the excludedFromEqualsAndHashCode rule. These fields can be either listed in an object-level array or flagged individually.
  921.  
  922. This option is only meaningful if equals() and hashCode() generation is enabled.
  923.  
  924. {
  925. "type": "object",
  926. "excludedFromEqualsAndHashCode" : [ "excludedByArray" ],
  927. "properties": {
  928. "notExcluded" : {
  929. "type" : "string"
  930. },
  931. "excludedByProperty" : {
  932. "type" : "string",
  933. "excludedFromEqualsAndHashCode" : true
  934. }
  935. }
  936. In the above example, only notExcluded will be inlcuded in equals() and hashCode().
  937.  
  938. © 2012-2018 Joe Littlejohn
  939.  
  940. Показывать эту иконку когда возможно
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement