users@jersey.java.net

Re: [Jersey] Jsonp issue:jaxb can't convert to json object to user client.

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Mon, 09 Mar 2009 16:35:16 +0100

Hi, please see inline...

On Tue, Mar 03, 2009 at 01:53:42AM -0800, beanor wrote:
>
> hi
>
> Follow is my code:
>
> Resouce class file:
>
> @Path("/jsonp")
> @Produces("application/x-javascript")
> public class JsonpResource {
> private static final List<Person> persons = new ArrayList<Person>();
> static {
> persons.add(new Person("zhangsan","women","11"));
> persons.add(new Person("lisi","man","12"));
> persons.add(new Person("wangwu","women","21"));
> }
> @GET
> @Path("/string")
> public JSONWithPadding getString(@QueryParam("callback") String
> callback) {
> return new JSONWithPadding("'helloWorld'", callback);
> }
>
> @GET
> @Path("/json")
> public JSONWithPadding getJson(@QueryParam("callback") String callback)
> throws Exception {
> JSONObject obj = new JSONObject();
> obj.put("name1", "jackDou");
> obj.put("name2", "redhacker");
> obj.put("name3", "beanor");
> return new JSONWithPadding(obj, callback);
> }
>
> @GET
> @Path("/jaxb")
> public JSONWithPadding getJaxb(@QueryParam("callback") String callback)
> throws Exception {
> return new JSONWithPadding(new
> GenericEntity<List<Person>>(persons){}, callback);
> }
> }
>
> Jaxb class file:
>
> package com.jersey.sample.model;
>
> import java.util.HashSet;
> import java.util.Set;
>
> import javax.xml.bind.annotation.XmlAccessType;
> import javax.xml.bind.annotation.XmlAccessorType;
> import javax.xml.bind.annotation.XmlRootElement;
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlRootElement(name = "person")
> public class Person {
> private String name;
> private String sex;
> private String age;
> private int mark;
> private Set<Child> childSet = new HashSet<Child>();

i do not see Child class used above source code

> public Person(String name, String sex, String age) {
> super();
> this.name = name;
> this.sex = sex;
> this.age = age;
> }
> public String getName() {
> return name;
> }
> public void setName(String name) {
> this.name = name;
> }
> public String getSex() {
> return sex;
> }
> public void setSex(String sex) {
> this.sex = sex;
> }
> public String getAge() {
> return age;
> }
> public void setAge(String age) {
> this.age = age;
> }

you will need also an explicit no-arg ctor to make jaxb happy

> /**
> * @return the childSet
> */
> public Set<Child> getChildSet() {
> return childSet;
> }
> /**
> * @param childSet the childSet to set
> */
> public void setChildSet(Set<Child> childSet) {
> this.childSet = childSet;
> }
> /**
> * @return the mark
> */
> public int getMark() {
> return mark;
> }
> /**
> * @param mark the mark to set
> */
> public void setMark(int mark) {
> this.mark = mark;
> }
> }
>
> Provider class file:
>
> @Provider
> public final class JAXBContextResolver implements
> ContextResolver<JAXBContext> {
>
> private final JAXBContext context;
>
> private final Set<Class> types;
>
> private final Class[] cTypes = {Person.class};

Since you are using Child above, you should be initializing the context
with both classes (cTypes = {Person.class, Child.class}

>
> public JAXBContextResolver() throws Exception {
> this.types = new HashSet(Arrays.asList(cTypes));
> this.context = new
> JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
> }
>
> public JAXBContext getContext(Class<?> objectType) {
> return (types.contains(objectType)) ? context : null;
> }
> }
>
> Jsp File:
>
> <%@ page language="java" contentType="text/html; charset=utf-8"
> pageEncoding="utf-8"%>
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
> <title>Insert title here</title>
> </head>
> <body>
> <script type="text/javascript">
> function stringCallback(stringObj) {
> document.writeln("<p>============== stringCallback result =========</p>");
> document.writeln(stringObj);
> }
> function jsonCallback(jsonObj) {
> document.writeln("<p>============== jsonCallback result =========</p>");
> document.writeln(jsonObj["name1"]);
> document.writeln(jsonObj.name2);
> document.writeln(jsonObj.name3)
> }
> function jaxbCallback(jaxbObj) {
> document.writeln("<p>============== jaxbCallback result =========</p>");
> document.writeln(jaxbObj);
> }
> </script>
> <script type="text/javascript"
> src="/jerseydemo/resources/jsonp/string?callback=stringCallback"></script>
> <script type="text/javascript"
> src="/jerseydemo/resources/jsonp/json?callback=jsonCallback"></script>
> <script type="text/javascript"
> src="/jerseydemo/resources/jsonp/jaxb?callback=jaxbCallback"></script>
> </body>
> </html>
>
> Run Console log:
>
> 2009-3-3 17:35:24
> com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider writeTo
> 严重: [failed to localize]
> error.jsonp.msg.body.writer.not.found([com.jersey.sample.model.Person_at_1735602,
> com.jersey.sample.model.Person_at_113cf49,
> com.jersey.sample.model.Person_at_18df055], application/json)

This is strange, when i am trying without Child specific elements (i have added Person
class to jsonp example), it works fine for me:

%curl -HAccept:application/x-javascript http://localhost:9998/jsonp/changes/jaxb
callback({"person":[{"name":"zhangsan","sex":"women","age":"11","mark":"0"},{"name":"lisi","sex":"man","age":"12","mark":"0"},{"name":"wangwu","sex":"women","age":"21","mark":"0"}]})%

Could you please try to add the Child class to your jaxb resolver and report back.

Also please feel free to report an issue[1] with your test case attached,
if you still having difficulties.

HTH,

~Jakub

[1]https://jersey.dev.java.net/issues/

> 2009-3-3 17:35:24 com.sun.jersey.server.impl.application.WebApplicationImpl
> onException
> 严重: Internal server error
> javax.ws.rs.WebApplicationException
> at
> com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider.writeTo(JSONWithPaddingProvider.java:127)
> at
> com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider.writeTo(JSONWithPaddingProvider.java:67)
> at
> com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:254)
> at
> com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:578)
> at
> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:502)
> at
> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:493)
> at
> com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:308)
> at
> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:314)
> at
> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:239)
> at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at com.jersey.sample.filter.AuthFilter.doFilter(AuthFilter.java:38)
> at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
> at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
> at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
> at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
> at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
> at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
> at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
> at
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
> at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
> at java.lang.Thread.run(Thread.java:595)
>
>
> (1)<script type="text/javascript"
> src="/jerseydemo/resources/jsonp/string?callback=stringCallback"></script>
> (2)<script type="text/javascript"
> src="/jerseydemo/resources/jsonp/json?callback=jsonCallback"></script>
> (3)<script type="text/javascript"
> src="/jerseydemo/resources/jsonp/jaxb?callback=jaxbCallback"></script>
>
> (1)(2) are working nice,But (3) is working bad.
>
> any help?
> --
> View this message in context: http://n2.nabble.com/Jsonp-issue%3Ajaxb-can%27t-convert-to-json-object-to-user-client.-tp2414324p2414324.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>