|
From: Solomon D. <sd...@gm...> - 2008-10-30 00:08:13
|
Is there anyway to get something like this to work with JAXB? Can a
"Providers" use JPA/Hibernate to convert an object from and to a URL?
-Solomon
On Wed, Oct 29, 2008 at 5:09 PM, Bill Burke <bb...@re...> wrote:
> Implemented in Trunk:
>
> @PathParam, @QueryParam, @MatrixParam, @FormParam, and @HeaderParam are
> represented as strings in a raw HTTP request. The specification says
> that these types of injected parameters can be converted to objects if
> these objects have a valueOf(String) static method or a constructor that
> takes one String parameter. What if you have a class where valueOf() or
> this string constructor doesn't exist or is inappropriate for an HTTP
> request? Resteasy has a proprietary @Provider interface that you can
> plug in:
>
> package org.jboss.resteasy.spi;
>
> public interface StringConverter<T>
> {
> T fromString(String str);
>
> String toString(T value);
> }
>
>
> You implement this interface to provide your own custom string
> marshalling. It is registered within your web.xml under the
> resteasy.providers context-param (See Installation and Configuration
> chapter). You can do it manually by calling the
> ResteasyProviderFactory.addStringConverter() method. Here's a simple
> example of using a StringConverter:
>
> import org.jboss.resteasy.client.ProxyFactory;
> import org.jboss.resteasy.spi.StringConverter;
> import org.jboss.resteasy.test.BaseResourceTest;
> import org.junit.Assert;
> import org.junit.Before;
> import org.junit.Test;
>
> import javax.ws.rs.HeaderParam;
> import javax.ws.rs.MatrixParam;
> import javax.ws.rs.PUT;
> import javax.ws.rs.Path;
> import javax.ws.rs.PathParam;
> import javax.ws.rs.QueryParam;
> import javax.ws.rs.ext.Provider;
>
> public class StringConverterTest extends BaseResourceTest
> {
> public static class POJO
> {
> private String name;
>
> public String getName()
> {
> return name;
> }
>
> public void setName(String name)
> {
> this.name = name;
> }
> }
>
> @Provider
> public static class POJOConverter implements StringConverter<POJO>
> {
> public POJO fromString(String str)
> {
> System.out.println("FROM STRNG: " + str);
> POJO pojo = new POJO();
> pojo.setName(str);
> return pojo;
> }
>
> public String toString(POJO value)
> {
> return value.getName();
> }
> }
>
> @Path("/")
> public static class MyResource
> {
> @Path("{pojo}")
> @PUT
> public void put(@QueryParam("pojo")POJO q,
> @PathParam("pojo")POJO pp,
> @MatrixParam("pojo")POJO mp,
> @HeaderParam("pojo")POJO hp)
> {
> Assert.assertEquals(q.getName(), "pojo");
> Assert.assertEquals(pp.getName(), "pojo");
> Assert.assertEquals(mp.getName(), "pojo");
> Assert.assertEquals(hp.getName(), "pojo");
> }
> }
>
> @Before
> public void setUp() throws Exception
> {
>
> dispatcher.getProviderFactory().addStringConverter(POJOConverter.class);
> dispatcher.getRegistry().addPerRequestResource(MyResource.class);
> }
>
> @Path("/")
> public static interface MyClient
> {
> @Path("{pojo}")
> @PUT
> void put(@QueryParam("pojo")POJO q, @PathParam("pojo")POJO pp,
> @MatrixParam("pojo")POJO mp, @HeaderParam("pojo")POJO
> hp);
> }
>
> @Test
> public void testIt() throws Exception
> {
> MyClient client = ProxyFactory.create(MyClient.class,
> "http://localhost:8081");
> POJO pojo = new POJO();
> pojo.setName("pojo");
> client.put(pojo, pojo, pojo, pojo);
> }
> }
>
>
>
> Bill Burke wrote:
> > I don't want to overload HeaderDelegate.
> >
> > I will create a new @Provider interface for this. Something like
> >
> > public interface StringConverter<T>
> > {
> > T fromString(String val);
> > String toString(T val);
> > }
> >
> > And then allow its use with @HeaderParam, @QueryParam, @PathParam,
> > @MatrixParam, and @FormParam injected objects.
> >
> > Sound good?
> >
> > Michael Brackx wrote:
> >> That would be nice.
> >> How could a HeaderDelegate be "activated" for a parameter?
> >>
> >> Michael
> >>
> >> On Fri, Oct 24, 2008 at 6:19 PM, Bill Burke <bb...@re...> wrote:
> >>> There's is HeaderDelegate in the specification. I could make sure that
> is
> >>> used for all String parameters.
> >>>
> >>> Michael Brackx wrote:
> >>>> Hi,
> >>>>
> >>>> It would be nice to have some sort of custom parameter unmarshalling.
> >>>> Currently parameter types can be String, primitive, or a class that
> >>>> has a String constructor or static valueOf(String) method.
> >>>> Some use cases:
> >>>> - injecting interfaces
> >>>> - injecting Enums (they already have a static valueOf(String) method)
> >>>> - injecting thirdparty classes
> >>>> - injecting factory created objects
> >>>>
> >>>> example:
> >>>>
> >>>> @GET
> >>>> @Path("/book/{isbn}")
> >>>> public String getBook(@PathParm(value = "isbn", provider =
> >>>> ISBNFactory.class) ISBN id) {
> >>>> ...
> >>>> }
> >>>>
> >>>> @Provider
> >>>> public class ISBNFactory {
> >>>> @ProviderMethod
> >>>> public ISBN create(String value) {
> >>>> return new ISBNImpl(value);
> >>>> }
> >>>> }
> >>>>
> >>>> The @ProviderMethod could be left out if there is only one matching
> >>>> method.
> >>>> Or a new interface could be used, to be more in line with
> ExceptionMapper.
> >>>>
> >>>> I realize the spec is final, but someday the will be a 1.1 or 2.0 :)
> >>>> Any comments?
> >>>>
> >>>> Michael Brackx
> >>>>
> >>>>
> -------------------------------------------------------------------------
> >>>> This SF.Net email is sponsored by the Moblin Your Move Developer's
> >>>> challenge
> >>>> Build the coolest Linux based applications with Moblin SDK & win great
> >>>> prizes
> >>>> Grand prize is a trip for two to an Open Source event anywhere in the
> >>>> world
> >>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>>> _______________________________________________
> >>>> Resteasy-developers mailing list
> >>>> Res...@li...
> >>>> https://lists.sourceforge.net/lists/listinfo/resteasy-developers
> >>> --
> >>> Bill Burke
> >>> JBoss, a division of Red Hat
> >>> http://bill.burkecentral.com
> >>>
> >
>
> --
> Bill Burke
> JBoss, a division of Red Hat
> http://bill.burkecentral.com
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Resteasy-developers mailing list
> Res...@li...
> https://lists.sourceforge.net/lists/listinfo/resteasy-developers
>
|