You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@wink.apache.org by Scott McMasters <sc...@gmail.com> on 2016/03/24 18:31:51 UTC
creating POJO's as REST parameters
Hello, all,
Is there any way to have Wink automatically create an instance of a POJO
and copy HTTP request parameter values, say, via BeanUtils, to a service
method parameter?
I'm thinking of something like this:
@POST
public void save(MyBean bean)
{
}
Thanks,
Scott McMasters
Re: creating POJO's as REST parameters
Posted by Darin Amos <da...@gmail.com>.
Hi Scott,
You can absolutely do what your asking.
First, for your MyBean, you want to look into creating custom Providers. You can create a custom provider for MyBean that is created from the request body. You should be able to find that documentation wth some google searches. You can also use the @HeaderParam annotation to get your header values.
You can reasonably have a classes such as:
@Path(“/test")
public class MyService {
@GET
@Consumes(MediaType.APPLICATION_JSON)
public String get(MyBean bean, @HeaderParam("Referer") String referer) {
...
}
}
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public final class MyBeanProvider implements MessageBodyReader<MyBean>{
@Override
public MyBean readFrom(Class<MyBean> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inputStream) throws IOException {
//Code to build and return MyBean using request input stream
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true;
}
@Override
public long getSize(ANFRestResource instance, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
return -1;
}
}
Cheers
Darin
> On Mar 24, 2016, at 10:31 AM, Scott McMasters <sc...@gmail.com> wrote:
>
> Hello, all,
>
> Is there any way to have Wink automatically create an instance of a POJO and copy HTTP request parameter values, say, via BeanUtils, to a service method parameter?
>
> I'm thinking of something like this:
>
> @POST
> public void save(MyBean bean)
> {
> }
>
> Thanks,
>
> Scott McMasters
>