{"id":35,"date":"2010-10-08T11:01:30","date_gmt":"2010-10-08T10:01:30","guid":{"rendered":"http:\/\/www.flamingpenguin.co.uk\/blog\/?p=35"},"modified":"2010-10-08T11:01:30","modified_gmt":"2010-10-08T10:01:30","slug":"marshalling-immutable-types-in-jaxb","status":"publish","type":"post","link":"https:\/\/www.flamingpenguin.co.uk\/blog\/2010\/10\/08\/marshalling-immutable-types-in-jaxb\/","title":{"rendered":"Marshalling Immutable Types in Jaxb"},"content":{"rendered":"<p>Unfortunately Jaxb requires any classes it creates during unmarshalling to have a default no-arguments constructor, and some way of mutating the fields. This prevents the use of types which enforce immutability using final fields.<\/p>\n<p>A way to get around this is to use the <a href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/javax\/xml\/bind\/annotation\/adapters\/XmlJavaTypeAdapter.html\">@XmlJavaTypeAdapter<\/a> annotation and implement custom marshalling and unmarshalling.<\/p>\n<p>For example, create a class like this:<\/p>\n<pre lang=\"java\">\r\n@XmlJavaTypeAdapter(value = MyValueXmlAdapter.class)\r\npublic class MyValue\r\n{\r\n   private final String value;\r\n\r\n   private MyValue(final String value)\r\n   {\r\n      this.value = value;\r\n   }\r\n\r\n   @Override\r\n   public String toString()\r\n   {\r\n      return value;\r\n   }\r\n\r\n   public static MyValue valueOf(final String value)\r\n   {\r\n      return new MyValue(value);\r\n   }\r\n}\r\n<\/pre>\n<p>and another like this:<\/p>\n<pre lang=\"java\">\r\npublic class MyValueXmlAdapter extends XmlAdapter<String, MyValue>\r\n{\r\n   @Override\r\n   public String marshal(final MyValue value) throws Exception\r\n   {\r\n      return value.toString();\r\n   }\r\n\r\n   @Override\r\n   public MyValue unmarshal(final String value) throws Exception\r\n   {\r\n      return MyValue.valueOf(value);\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Unfortunately Jaxb requires any classes it creates during unmarshalling to have a default no-arguments constructor, and some way of mutating the fields. This prevents the use of types which enforce immutability using final fields. A way to get around this &hellip; <a href=\"https:\/\/www.flamingpenguin.co.uk\/blog\/2010\/10\/08\/marshalling-immutable-types-in-jaxb\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,26],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-java","category-jaxb"],"_links":{"self":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}