|
From: Jesus M. R. <jm...@gm...> - 2010-03-15 18:55:46
|
I'm looking at switching from jersey to resteasy. I have an object
that is annotated with jaxb.
I have a TestResource class that I want to produce both XML and natural json.
@GET
@Path("/gettest")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public JsonTestObject gettest() {
JsonTestObject parent1 = new JsonTestObject();
parent1.setName("parent1");
parent1.setStringList(new ArrayList<String>());
parent1.setParent(null);
List<String> stringlist = new ArrayList<String>();
stringlist.add("string2");
stringlist.add("string3");
JsonTestObject parent = new JsonTestObject();
parent.setName("parentname");
parent.setParent(parent1);
parent.setStringList(stringlist);
stringlist.add("child");
JsonTestObject jto1 = new JsonTestObject();
jto1.setName("myname");
jto1.setParent(parent);
jto1.setStringList(stringlist);
return jto1;
}
In jersey, I get natural json:
testjsonobject get:
{"name":"myname","parent":{"name":"parentname","parent":{"name":"parent1"},"stringList":["string2","string3","child"]},"stringList":["string2","string3","child"]}
but with resteasy, I get the mapped jettison format:
testjsonobject get:
{"jsontest":{"name":"myname","parent":[{"name":"parentname"},{"name":"parent1"}]},"stringList":["string2","string3","child","string2","string3","child"]}
Is there a way to get the xml and "natural" json support with resteasy?
Sincerely,
jesus rodriguez
|
|
From: Jesus M. R. <jm...@gm...> - 2010-03-15 20:26:23
|
Lars, Sure here's the JsonTestObject code: http://pastie.org/870667 jesus On Mon, Mar 15, 2010 at 4:20 PM, Lars Ræder Clausen <lr...@am...> wrote: > Yes, there is, we do it. But it might interfere with the way you get > your XML. Without seeing your JsonTestObject class, I can't tell why > it's doing it that way. > > -Lars > > On Mon, Mar 15, 2010 at 7:55 PM, Jesus M. Rodriguez <jm...@gm...> wrote: >> I'm looking at switching from jersey to resteasy. I have an object >> that is annotated with jaxb. >> I have a TestResource class that I want to produce both XML and natural json. >> >> @GET >> @Path("/gettest") >> @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) >> public JsonTestObject gettest() { >> >> JsonTestObject parent1 = new JsonTestObject(); >> parent1.setName("parent1"); >> parent1.setStringList(new ArrayList<String>()); >> parent1.setParent(null); >> >> List<String> stringlist = new ArrayList<String>(); >> stringlist.add("string2"); >> stringlist.add("string3"); >> JsonTestObject parent = new JsonTestObject(); >> parent.setName("parentname"); >> parent.setParent(parent1); >> parent.setStringList(stringlist); >> >> stringlist.add("child"); >> JsonTestObject jto1 = new JsonTestObject(); >> jto1.setName("myname"); >> jto1.setParent(parent); >> jto1.setStringList(stringlist); >> >> >> return jto1; >> } >> >> In jersey, I get natural json: >> >> testjsonobject get: >> {"name":"myname","parent":{"name":"parentname","parent":{"name":"parent1"},"stringList":["string2","string3","child"]},"stringList":["string2","string3","child"]} >> >> but with resteasy, I get the mapped jettison format: >> >> testjsonobject get: >> {"jsontest":{"name":"myname","parent":[{"name":"parentname"},{"name":"parent1"}]},"stringList":["string2","string3","child","string2","string3","child"]} >> >> Is there a way to get the xml and "natural" json support with resteasy? >> >> Sincerely, >> jesus rodriguez >> >> ------------------------------------------------------------------------------ >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and fine-tune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intel-sw-dev >> _______________________________________________ >> Resteasy-users mailing list >> Res...@li... >> https://lists.sourceforge.net/lists/listinfo/resteasy-users >> > |
|
From: Jesus M. R. <jm...@gm...> - 2010-03-16 12:59:00
|
Thanks for responding again Lars. If I take out (name = "jsontest"), the resulting JSON actually uses the classname: jsonTestObject. Any other ideas as to how to get it to generate "natural" json? jesus On Tue, Mar 16, 2010 at 2:48 AM, Lars Ræder Clausen <lr...@am...> wrote: > I would say it's because you're naming the object in @XmlRootElement. > > -Lars > > On Mon, Mar 15, 2010 at 9:25 PM, Jesus M. Rodriguez <jm...@gm...> wrote: >> Lars, >> >> Sure here's the JsonTestObject code: http://pastie.org/870667 >> >> jesus >> >> On Mon, Mar 15, 2010 at 4:20 PM, Lars Ræder Clausen <lr...@am...> wrote: >>> Yes, there is, we do it. But it might interfere with the way you get >>> your XML. Without seeing your JsonTestObject class, I can't tell why >>> it's doing it that way. >>> >>> -Lars >>> >>> On Mon, Mar 15, 2010 at 7:55 PM, Jesus M. Rodriguez <jm...@gm...> wrote: >>>> I'm looking at switching from jersey to resteasy. I have an object >>>> that is annotated with jaxb. >>>> I have a TestResource class that I want to produce both XML and natural json. >>>> >>>> @GET >>>> @Path("/gettest") >>>> @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) >>>> public JsonTestObject gettest() { >>>> >>>> JsonTestObject parent1 = new JsonTestObject(); >>>> parent1.setName("parent1"); >>>> parent1.setStringList(new ArrayList<String>()); >>>> parent1.setParent(null); >>>> >>>> List<String> stringlist = new ArrayList<String>(); >>>> stringlist.add("string2"); >>>> stringlist.add("string3"); >>>> JsonTestObject parent = new JsonTestObject(); >>>> parent.setName("parentname"); >>>> parent.setParent(parent1); >>>> parent.setStringList(stringlist); >>>> >>>> stringlist.add("child"); >>>> JsonTestObject jto1 = new JsonTestObject(); >>>> jto1.setName("myname"); >>>> jto1.setParent(parent); >>>> jto1.setStringList(stringlist); >>>> >>>> >>>> return jto1; >>>> } >>>> >>>> In jersey, I get natural json: >>>> >>>> testjsonobject get: >>>> {"name":"myname","parent":{"name":"parentname","parent":{"name":"parent1"},"stringList":["string2","string3","child"]},"stringList":["string2","string3","child"]} >>>> >>>> but with resteasy, I get the mapped jettison format: >>>> >>>> testjsonobject get: >>>> {"jsontest":{"name":"myname","parent":[{"name":"parentname"},{"name":"parent1"}]},"stringList":["string2","string3","child","string2","string3","child"]} >>>> >>>> Is there a way to get the xml and "natural" json support with resteasy? >>>> >>>> Sincerely, >>>> jesus rodriguez >>>> >>>> ------------------------------------------------------------------------------ >>>> Download Intel® Parallel Studio Eval >>>> Try the new software tools for yourself. Speed compiling, find bugs >>>> proactively, and fine-tune applications for parallel performance. >>>> See why Intel Parallel Studio got high marks during beta. >>>> http://p.sf.net/sfu/intel-sw-dev >>>> _______________________________________________ >>>> Resteasy-users mailing list >>>> Res...@li... >>>> https://lists.sourceforge.net/lists/listinfo/resteasy-users >>>> >>> >> >> ------------------------------------------------------------------------------ >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and fine-tune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intel-sw-dev >> _______________________________________________ >> Resteasy-users mailing list >> Res...@li... >> https://lists.sourceforge.net/lists/listinfo/resteasy-users >> > |
|
From: Jesus M. R. <jm...@gm...> - 2010-03-16 13:33:41
|
On Tue, Mar 16, 2010 at 9:05 AM, Lars Ræder Clausen <lr...@am...> wrote:
> Could be something that the RestEasy-modified version of Jackson
> causes. Here's a sample web service of mine:
>
> Interface:
> @Path("/moduletype")
> public interface ModuleTypeWS {
> @GET
> @Path("/{id:[0-9]*}")
> @Produces({"application/xml","application/json"})
> public ModuleType get(@PathParam("id") long id);
> }
>
> Implementation:
> @Stateless
> public class ModuleTypeWSBean implements ModuleTypeWS {
> @EJB private ModuleTypeDao moduleTypeDao;
>
> @Override
> public ModuleType get(@PathParam("id") long id) {
> final ModuleType type = moduleTypeDao.get(id);
> return type;
> }
> }
>
> Its output looks like this (with pretty-printing turned on):
>
> {
> "name" : "Elster A1800",
> "resource" : [ {
> "moduleType" : {
> "id" : "106"
> }},
> ...
> ]
> }
>
> I'm using the JacksonJaxbJsonProvider that comes with Jackson.
Maybe that's the trick. That's pretty much what I want but my
project is not using jackson right now.
jesus
|