I'm not sure but your error message:
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)
Indicates it can't find a provider with a MessageBodyWriter that 'produces' application/json... The packaged JSONJAXBContext in your @Provider supplies a JSON MessageBodyWriter but the resource matching for application/x-javascript isn't understood by that writer. Your resource class is annotated:
@Path("/jsonp")
@Produces("application/x-javascript")
public class JsonpResource {
I think it should be:
@Path("/jsonp")
@Produces("application/json")
public class JsonpResource {
Or you can annotate the specific method:
@GET @Path("/json")
@Produces("application/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);
}
--mark
_______________________________________________
Mark A. Rabick - Software Engineer
Em: mark.rabick_at_ngc.com
> -----Original Message-----
> From: beanor [mailto:beanor_at_gmail.com]
> Sent: Tuesday, March 03, 2009 3:54 AM
> To: users_at_jersey.dev.java.net
> Subject: [Jersey] Jsonp issue:jaxb can't convert to json
> object to user client.
>
>
> 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>();
> 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;
> }
> /**
> * @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};
>
> 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=stringCallbac
k"></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.JSONWithPaddingProvid
> er 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)
> 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.JSONWithPaddingProvid
> er.writeTo(JSONWithPaddingProvider.java:127)
> at
> com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvid
> er.writeTo(JSONWithPaddingProvider.java:67)
> at
> com.sun.jersey.spi.container.ContainerResponse.write(Container
> Response.java:254)
> at
> com.sun.jersey.server.impl.application.WebApplicationImpl._han
dleRequest(WebApplicationImpl.java:578)
> at
> com.sun.jersey.server.impl.application.WebApplicationImpl.hand
> leRequest(WebApplicationImpl.java:502)
> at
> com.sun.jersey.server.impl.application.WebApplicationImpl.hand
> leRequest(WebApplicationImpl.java:493)
> at
> com.sun.jersey.spi.container.servlet.WebComponent.service(WebC
> omponent.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.internalDoFilt
> er(ApplicationFilterChain.java:290)
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(Appli
> cationFilterChain.java:206)
> at
> com.jersey.sample.filter.AuthFilter.doFilter(AuthFilter.java:38)
> at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilt
> er(ApplicationFilterChain.java:235)
> at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(Appli
> cationFilterChain.java:206)
> at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardW
> rapperValve.java:233)
> at
> org.apache.catalina.core.StandardContextValve.invoke(StandardC
> ontextValve.java:175)
> at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHost
> Valve.java:128)
> at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReport
> Valve.java:102)
> at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEn
> gineValve.java:109)
> at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdap
> ter.java:263)
> at
> org.apache.coyote.http11.Http11Processor.process(Http11Process
> or.java:844)
> at
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandle
r.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=stringCallbac
k"></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-jso
n-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
>
>
>