tag:blogger.com,1999:blog-1710464868724994296.post1623769360135287814..comments2023-06-29T09:48:09.653-04:00Comments on Java XML and JSON Binding: MOXy as Your JAX-RS JSON Provider - Server SideBlaise Doughanhttp://www.blogger.com/profile/02016807982653343777noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-1710464868724994296.post-74501428220442545452013-06-19T16:05:34.073-04:002013-06-19T16:05:34.073-04:00I currently do not have the files for this example...I currently do not have the files for this example hosted anywhere.Blaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-55140332235654925372013-06-18T09:22:31.525-04:002013-06-18T09:22:31.525-04:00Hello,
Couldn't it be possible to download the...Hello,<br />Couldn't it be possible to download the example? I'm trying to use MOXy with Spring Hibernate on Apache Tomcat!<br />ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-84179841748339205942013-04-03T11:40:26.862-04:002013-04-03T11:40:26.862-04:00This issue does not exist in MOXy's JSON bindi...This issue does not exist in MOXy's JSON binding. The easiest way to enable MOXy as your JSON provider is to use the <i>MOXyJsonProvider</i> class:<br />- <a href="http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html" rel="nofollow">MOXy as Your JAX-RS JSON Provider - MOXyJsonProvider</a>Blaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-34574130635075565732013-04-03T04:49:33.556-04:002013-04-03T04:49:33.556-04:00Question: Did MOXy already solve this problem? Nul...Question: Did MOXy already solve this problem? <a href="http://java.net/jira/browse/JERSEY-339" rel="nofollow">Null @XmlRootElement produces null instead of empty JSON</a> I'm facing this problem with GlassFish 3.1.2.2 (embedded).Jin Kwonhttps://www.blogger.com/profile/13937022159401759104noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-55938235942234001262013-01-05T05:50:01.019-05:002013-01-05T05:50:01.019-05:00Hi Suku,
I would recommend using the MOXyJsonProv...Hi Suku,<br /><br />I would recommend using the MOXyJsonProvider class. This way you can enable it for you entire application:<br />- <a href="http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html" rel="nofollow">MOXy as Your JAX-RS JSON Provider - MOXyJsonProvider</a><br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-52729497927053686342013-01-01T22:59:06.366-05:002013-01-01T22:59:06.366-05:00Hi Blaise,
The CustomerJSONProvider.java is worki...Hi Blaise,<br /><br />The CustomerJSONProvider.java is working fine for single class ( return Customer.class == getDomainClass(genericType); ). Suppose if we want to apply for multiple classes (pacakge)how can we proceed.<br /><br />Thanks in advance...<br /><br />KumarAnonymoushttps://www.blogger.com/profile/00098628335426294627noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-65291001202777089722012-05-29T11:45:58.041-04:002012-05-29T11:45:58.041-04:00Hi Gregor,
It probably isn't that my JPA quer...Hi Gregor,<br /><br />It probably isn't that my JPA query is returning something different from yours, but the return type on the <i>findCustomersByCity</i> method is parameterized.<br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-18171109252074783392012-05-29T11:37:38.538-04:002012-05-29T11:37:38.538-04:00Hi Gregor,
Thank you for pointing out this aspect...Hi Gregor,<br /><br />Thank you for pointing out this aspect. I have updated my example to include it.<br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-10310737751390608022012-05-28T05:00:43.850-04:002012-05-28T05:00:43.850-04:00Hi Blaise,
In my tests I found that I have to expl...Hi Blaise,<br />In my tests I found that I have to explicitly set the characterset when reading from the stream, otherwise the German umlauts get misinterpreted (the same with French accents, I think). So I changed the unmarshaling part in the readFrom like the following:<br /><br /> Reader reader;<br /> if (mediaType.getParameters().containsKey("charset")) {<br /> reader = new InputStreamReader(entityStream, mediaType<br /> .getParameters().get("charset"));<br /> } else {<br /> reader = new InputStreamReader(entityStream);<br /> }<br /> return u.unmarshal(new StreamSource(reader), domainClass)<br /> .getValue();<br /><br />Ciao<br />GregorAnonymoushttps://www.blogger.com/profile/04755036412507835130noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-22796681640217533512012-05-28T04:43:39.194-04:002012-05-28T04:43:39.194-04:00Hi Blaise,
in my last reply I mistook isReadable f...Hi Blaise,<br />in my last reply I mistook isReadable for isWritable and readFrom for writeTo. For the GET, of course, the writing case is relevant. Sorry!<br />And in addition to the change I did to the getDomainClass method I also changed the isWritable to always return true. This is not a real satisfying solution, just a workaround. <br />Ciao<br />GregorAnonymoushttps://www.blogger.com/profile/04755036412507835130noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-14713905156393186492012-05-28T04:03:57.959-04:002012-05-28T04:03:57.959-04:00Hi Blais,
let's assume I want to return a Vect...Hi Blais,<br />let's assume I want to return a Vector of Customers from my Resource Implementation, as in the following example:<br /><br />@Path("/customer")<br />public class CustomerResource {<br /> @GET<br /> @Path("/list")<br /> @Produces(MediaType.APPLICATION_JSON)<br /> public Response get() {<br /> Vector customers = new Vector();<br /> Customer customer = new Customer("Fridolin Fröhlich", "4711");<br /> customers.add(customer);<br /> return Response.ok(customers).build();<br /> }<br />}<br /><br />When I invoke this I get an MessageException with the message: Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.Vector, and Java type class java.util.Vector, and MIME media type application/json was not found<br /><br />If I encapsulate the Vector within a javax.ws.rs.core.GenericEntity everything is okay:<br /><br /> GenericEntity> entities = new GenericEntity>(customers) {};<br /> return Response.ok(entities).build();<br /><br />The difference is, that in the later case, with the GenericEntity, the genericType is a ParameterizedType, the getDomainClass returns the actual domain type (Customer) and the JAXBContext creates an org.eclipse.persistence.jaxb.JAXBMarshaller, whereas with the plain Vector the genricType is a Class, the getDomainClass returns the Vector class, the isReadable returns false and our readFrom method is not called at all.<br />As I want Moxy also to marshall and unmarshall any Collections of my domain classes I changed the getDomainClass method in the following way:<br /><br /> private Class getDomainClass(Object object, Type genericType) {<br /> if (genericType instanceof ParameterizedType) {<br /> return (Class) ((ParameterizedType) genericType)<br /> .getActualTypeArguments()[0];<br /> } else if (object instanceof Collection) {<br /> Collection collection = (Collection) object;<br /> if (collection.isEmpty()) {<br /> return this.getClass();<br /> } else {<br /> return collection.toArray()[0].getClass();<br /> }<br /> } else if (genericType instanceof Class) {<br /> return (Class) genericType;<br /> } else {<br /> return null;<br /> }<br /> }<br /><br />This works quite well and allows me to get rid of the GenericEntity encapsulation.<br />So far so good. The only question that remains for me is, why the JPA query in your case returns a ParameterizedType, but in my case I get a Vector (which is what I expected). Do you have an idea?Anonymoushttps://www.blogger.com/profile/04755036412507835130noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-71841258602379385962012-05-25T14:35:30.835-04:002012-05-25T14:35:30.835-04:00Hi Gregor,
Yes, in my example the named query ret...Hi Gregor,<br /><br />Yes, in my example the named query returns a <i>ParaterizedType</i>. Can you provide more information about the <i>GenericEntity</i> that you return?<br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-13900323690059641042012-05-25T13:48:28.683-04:002012-05-25T13:48:28.683-04:00Hi Gregor,
You are correct. I have updated the e...Hi Gregor,<br /><br />You are correct. I have updated the example to explicitly set the media type on the <i>Marshaller</i>/<i>Unmarshaller</i> as <i>MediaType.APPLICATION_JSON</i>. MOXy now includes an implementation of <i>MessageBodyReader</i>/<i>MessageBodyWriter</i> to make things even easier:<br />- <a href="http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html" rel="nofollow">MOXy as Your JAX-RS JSON Provider - MOXyJsonProvider </a><br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-84942329355900133322012-05-20T03:57:38.195-04:002012-05-20T03:57:38.195-04:00Setting the property "eclipselink.media-type&...Setting the property "eclipselink.media-type" to "application/json; charset=utf-8" yields an exception. Thus the characterset must be stripped off before setting the property.Anonymoushttps://www.blogger.com/profile/04755036412507835130noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-88998359708769368982012-05-18T11:35:10.230-04:002012-05-18T11:35:10.230-04:00Hi Blaise,
great Blog. It's good to see that e...Hi Blaise,<br />great Blog. It's good to see that eclipselink is about to support JSON out of the box. I had no problems to transfer your example to my case. But there is one issue that makes me headaches already for a long time: How to marshal a collection correctly. In my case, if I do not wrap my collection with a "GenericEntity", the genericType is java.util.Vector, which is an instance of Class but not of ParameterizedType. Whereas the GenericEntity yields a ParameterizedTypeImpl. As a consequence the sun marshaller is used and not the moxy-one, which leads to an exception, when the eclipselink properties get set.<br />Do you have any idea how I could get rid of the GenericEntity?<br />Actually I wonder how it can work in your example. Does your named query return a ParameterizedType?Anonymoushttps://www.blogger.com/profile/04755036412507835130noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-25851948896115663562012-05-14T11:08:34.391-04:002012-05-14T11:08:34.391-04:00Hi Russ,
For the JSON-binding you need to use Ecl...Hi Russ,<br /><br />For the JSON-binding you need to use EclipseLink 2.4.0. You can download a pre-release version from the following link:<br />- <a href="http://www.eclipse.org/eclipselink/downloads/milestones.php" rel="nofollow">http://www.eclipse.org/eclipselink/downloads/milestones.php</a><br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-163653632423516032012-05-12T23:47:40.626-04:002012-05-12T23:47:40.626-04:00Hi Blaise,
I tried using your example with versio...Hi Blaise,<br /><br />I tried using your example with version 2.3.2 and the setProperty method on the JAXBMrshaller doesn't have either of the properties "eclipselink.json.include-root" or "eclipselink.media-type". Is something new required for version 2.3.2?<br /><br />ThanksAnonymoushttps://www.blogger.com/profile/05909095943450298616noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-42970946099553572402012-04-21T13:28:50.200-04:002012-04-21T13:28:50.200-04:00Hi Dmitry,
Yes, MOXy will format the JSON output ...Hi Dmitry,<br /><br />Yes, MOXy will format the JSON output if the standard Marshaller.JAXB_FORMATTED_OUTPUT flag is set. For an example see:<br />- <a href="http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html" rel="nofollow">JSON Binding with EclipseLink MOXy - Twitter Example</a><br /><br />-BlaiseBlaise Doughanhttps://www.blogger.com/profile/02016807982653343777noreply@blogger.comtag:blogger.com,1999:blog-1710464868724994296.post-66523592823935187922012-04-20T19:06:30.760-04:002012-04-20T19:06:30.760-04:00Will MOXy format the JSON output if Marshaller.JAX...Will MOXy format the JSON output if Marshaller.JAXB_FORMATTED_OUTPUT is set?Anonymoushttps://www.blogger.com/profile/01576799796127639511noreply@blogger.com