You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID" <dk...@att.com> on 2017/04/10 22:59:00 UTC

Better way to get post response parameters into a POJO?

I posted this on SO (http://stackoverflow.com/questions/43333855/using-cxf-clientbuilder-how-to-unmarshal-post-response-parameters-into-java-cla), but some people might not see it, but might see it here, so I'm asking here also.

To put it another way, I have the following code as a base:
-------------------
	public CodeAndMessage validateToken(String token) {
		WebTarget	target	= client.target(getHostPort()).path(getPath());
		Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
		Form  form    = new Form();
		form.param("TokenID", token);
		Form  responseForm    = request.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), Form.class);
		System.out.println("responseForm[" + responseForm + "] map[" + responseForm.asMap() + "]");
		return new CodeAndMessage().
		            errorCode(responseForm.asMap().getFirst("errorCode")).
		            errorMessage(responseForm.asMap().getFirst("errorMessage"));
	}
----------------

Where "CodeAndMessage" is a simple POJO with two fields.

This works, but I was wondering if there is a cleaner way to do this, to avoid having to use the Form response and getting the map values.

RE: Better way to get post response parameters into a POJO?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, April 11, 2017 11:26 AM
> To: users@cxf.apache.org
> Subject: Re: Better way to get post response parameters into a POJO?
> 
> The easiest way is to delegate to Cxf FormEncodingProvider (set to a
> field in your custom provider) and in your provider's readFrom you'd get
> the Form from the Cxf provider and convert HTH Sergey

I did notice FormEncodingProvider, but I didn't understand how to use it.  Is there an example that looks sort of like my problem?

> On Tuesday, April 11, 2017, KARR, DAVID <dk...@att.com> wrote:
> 
> > > -----Original Message-----
> > > From: Sergey Beryozkin [mailto:sberyozkin@gmail.com <javascript:;>]
> > > Sent: Tuesday, April 11, 2017 9:42 AM
> > > To: users@cxf.apache.org <javascript:;>
> > > Subject: Re: Better way to get post response parameters into a POJO?
> > >
> > > Hi, well, if the response data are in the form format then you need
> > > to read it as a form, but perhaps a cleaner way is to create
> > > MessageBodyReader for CodeAndMessage, which can convert a form
> > > response, Cheers Sergey
> >
> > I guess I was heading in that direction.  Are there examples of
> > creating a MessageBodyReader from a Form Map?  I'll start reading up
> > on writing MBRs, but it would be good if I had something similar to
> start with.
> >
> > > On Tuesday, April 11, 2017, KARR, DAVID <dk068x@att.com
> > > <javascript:;>>
> > wrote:
> > >
> > > > I posted this on SO
> > > > (https://urldefense.proofpoint.com/v2/url?u=http-3A__stackoverflow
> > > > .com
> > > > _&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-xy2uk0vYF_EA&m=t5H
> > > > 1VVQ
> > > > JcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=RZgen5CwkecIc_3aDZCfdv4Mggc
> > > > Ct_7
> > > > MwGPZWv3gLXw&e=
> > > > questions/43333855/using-cxf-clientbuilder-how-to-unmarshal-post-r
> > > > espo
> > > > nse- parameters-into-java-cla), but some people might not see it,
> > > > but might see it here, so I'm asking here also.
> > > >
> > > > To put it another way, I have the following code as a base:
> > > > -------------------
> > > >         public CodeAndMessage validateToken(String token) {
> > > >                 WebTarget       target  =
> > > client.target(getHostPort()).
> > > > path(getPath());
> > > >                 Builder request = target.request(MediaType.
> > > > APPLICATION_JSON_TYPE);
> > > >                 Form  form    = new Form();
> > > >                 form.param("TokenID", token);
> > > >                 Form  responseForm    =
> > > request.post(Entity.entity(form,
> > > > MediaType.APPLICATION_FORM_URLENCODED_TYPE), Form.class);
> > > >                 System.out.println("responseForm[" + responseForm
> > > > + "] map[" + responseForm.asMap() + "]");
> > > >                 return new CodeAndMessage().
> > > >                             errorCode(responseForm.asMap()
> > > > .getFirst("errorCode")).
> > > >
> > > errorMessage(responseForm.asMap().getFirst("
> > > > errorMessage"));
> > > >         }
> > > > ----------------
> > > >
> > > > Where "CodeAndMessage" is a simple POJO with two fields.
> > > >
> > > > This works, but I was wondering if there is a cleaner way to do
> > > > this, to avoid having to use the Form response and getting the map
> values.
> > > >
> > >
> > >
> > > --
> > > Sergey Beryozkin
> > >
> > > https://urldefense.proofpoint.com/v2/url?u=http-
> > > 3A__sberyozkin.blogspot.com&d=DwIBaQ&c=LFYZ-
> > > o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> > > xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=SjdRkGU
> > > eJQe
> > > WcTlvRwImNayrGo841bzo52GGjT61Tfs&e=
> > > Talend - https://urldefense.proofpoint.com/v2/url?u=http-
> > > 3A__www.talend.com&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> > > xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=Johgjpb
> > > XgXz
> > > hcMmAnCzGU135vYCDCXC6eiNQlciJx8U&e=
> >
> 
> 
> --
> Sergey Beryozkin
> 
> https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__sberyozkin.blogspot.com&d=DwIBaQ&c=LFYZ-
> o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> xy2uk0vYF_EA&m=Z3MDdgEEFVaho1KNQqcr1vnZLFJPCELnEzkCKwl-
> 6SM&s=_SkKKEEORoPi1yMyEAsc8zgCXkgMzzKV8YIM44MeeFY&e=
> Talend - https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__www.talend.com&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> xy2uk0vYF_EA&m=Z3MDdgEEFVaho1KNQqcr1vnZLFJPCELnEzkCKwl-
> 6SM&s=iF2VnZVk4kcGcibMMqo6-B0PpaQHMGH6JAwu_jQOJYg&e=

Re: Better way to get post response parameters into a POJO?

Posted by Sergey Beryozkin <sb...@gmail.com>.
The easiest way is to delegate to Cxf FormEncodingProvider (set to a field
in your custom provider) and in your provider's readFrom you'd get the Form
from the Cxf provider and convert
HTH Sergey

On Tuesday, April 11, 2017, KARR, DAVID <dk...@att.com> wrote:

> > -----Original Message-----
> > From: Sergey Beryozkin [mailto:sberyozkin@gmail.com <javascript:;>]
> > Sent: Tuesday, April 11, 2017 9:42 AM
> > To: users@cxf.apache.org <javascript:;>
> > Subject: Re: Better way to get post response parameters into a POJO?
> >
> > Hi, well, if the response data are in the form format then you need to
> > read it as a form, but perhaps a cleaner way is to create
> > MessageBodyReader for CodeAndMessage, which can convert a form response,
> > Cheers Sergey
>
> I guess I was heading in that direction.  Are there examples of creating a
> MessageBodyReader from a Form Map?  I'll start reading up on writing MBRs,
> but it would be good if I had something similar to start with.
>
> > On Tuesday, April 11, 2017, KARR, DAVID <dk068x@att.com <javascript:;>>
> wrote:
> >
> > > I posted this on SO
> > > (https://urldefense.proofpoint.com/v2/url?u=http-3A__stackoverflow.com
> > > _&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-xy2uk0vYF_EA&m=t5H1VVQ
> > > JcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=RZgen5CwkecIc_3aDZCfdv4MggcCt_7
> > > MwGPZWv3gLXw&e=
> > > questions/43333855/using-cxf-clientbuilder-how-to-unmarshal-post-respo
> > > nse- parameters-into-java-cla), but some people might not see it, but
> > > might see it here, so I'm asking here also.
> > >
> > > To put it another way, I have the following code as a base:
> > > -------------------
> > >         public CodeAndMessage validateToken(String token) {
> > >                 WebTarget       target  =
> > client.target(getHostPort()).
> > > path(getPath());
> > >                 Builder request = target.request(MediaType.
> > > APPLICATION_JSON_TYPE);
> > >                 Form  form    = new Form();
> > >                 form.param("TokenID", token);
> > >                 Form  responseForm    =
> > request.post(Entity.entity(form,
> > > MediaType.APPLICATION_FORM_URLENCODED_TYPE), Form.class);
> > >                 System.out.println("responseForm[" + responseForm + "]
> > > map[" + responseForm.asMap() + "]");
> > >                 return new CodeAndMessage().
> > >                             errorCode(responseForm.asMap()
> > > .getFirst("errorCode")).
> > >
> > errorMessage(responseForm.asMap().getFirst("
> > > errorMessage"));
> > >         }
> > > ----------------
> > >
> > > Where "CodeAndMessage" is a simple POJO with two fields.
> > >
> > > This works, but I was wondering if there is a cleaner way to do this,
> > > to avoid having to use the Form response and getting the map values.
> > >
> >
> >
> > --
> > Sergey Beryozkin
> >
> > https://urldefense.proofpoint.com/v2/url?u=http-
> > 3A__sberyozkin.blogspot.com&d=DwIBaQ&c=LFYZ-
> > o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> > xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=SjdRkGUeJQe
> > WcTlvRwImNayrGo841bzo52GGjT61Tfs&e=
> > Talend - https://urldefense.proofpoint.com/v2/url?u=http-
> > 3A__www.talend.com&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> > xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=JohgjpbXgXz
> > hcMmAnCzGU135vYCDCXC6eiNQlciJx8U&e=
>


-- 
Sergey Beryozkin

http://sberyozkin.blogspot.com
Talend - http://www.talend.com

RE: Better way to get post response parameters into a POJO?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, April 11, 2017 9:42 AM
> To: users@cxf.apache.org
> Subject: Re: Better way to get post response parameters into a POJO?
> 
> Hi, well, if the response data are in the form format then you need to
> read it as a form, but perhaps a cleaner way is to create
> MessageBodyReader for CodeAndMessage, which can convert a form response,
> Cheers Sergey

I guess I was heading in that direction.  Are there examples of creating a MessageBodyReader from a Form Map?  I'll start reading up on writing MBRs, but it would be good if I had something similar to start with.

> On Tuesday, April 11, 2017, KARR, DAVID <dk...@att.com> wrote:
> 
> > I posted this on SO
> > (https://urldefense.proofpoint.com/v2/url?u=http-3A__stackoverflow.com
> > _&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-xy2uk0vYF_EA&m=t5H1VVQ
> > JcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=RZgen5CwkecIc_3aDZCfdv4MggcCt_7
> > MwGPZWv3gLXw&e=
> > questions/43333855/using-cxf-clientbuilder-how-to-unmarshal-post-respo
> > nse- parameters-into-java-cla), but some people might not see it, but
> > might see it here, so I'm asking here also.
> >
> > To put it another way, I have the following code as a base:
> > -------------------
> >         public CodeAndMessage validateToken(String token) {
> >                 WebTarget       target  =
> client.target(getHostPort()).
> > path(getPath());
> >                 Builder request = target.request(MediaType.
> > APPLICATION_JSON_TYPE);
> >                 Form  form    = new Form();
> >                 form.param("TokenID", token);
> >                 Form  responseForm    =
> request.post(Entity.entity(form,
> > MediaType.APPLICATION_FORM_URLENCODED_TYPE), Form.class);
> >                 System.out.println("responseForm[" + responseForm + "]
> > map[" + responseForm.asMap() + "]");
> >                 return new CodeAndMessage().
> >                             errorCode(responseForm.asMap()
> > .getFirst("errorCode")).
> >
> errorMessage(responseForm.asMap().getFirst("
> > errorMessage"));
> >         }
> > ----------------
> >
> > Where "CodeAndMessage" is a simple POJO with two fields.
> >
> > This works, but I was wondering if there is a cleaner way to do this,
> > to avoid having to use the Form response and getting the map values.
> >
> 
> 
> --
> Sergey Beryozkin
> 
> https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__sberyozkin.blogspot.com&d=DwIBaQ&c=LFYZ-
> o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=SjdRkGUeJQe
> WcTlvRwImNayrGo841bzo52GGjT61Tfs&e=
> Talend - https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__www.talend.com&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=JohgjpbXgXz
> hcMmAnCzGU135vYCDCXC6eiNQlciJx8U&e=

RE: Better way to get post response parameters into a POJO?

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, April 11, 2017 9:42 AM
> To: users@cxf.apache.org
> Subject: Re: Better way to get post response parameters into a POJO?
> 
> Hi, well, if the response data are in the form format then you need to
> read it as a form, but perhaps a cleaner way is to create
> MessageBodyReader for CodeAndMessage, which can convert a form response,
> Cheers Sergey

Got it.  I found the "OOBResponseProvider" class in the CXF source, that sort of looked like what I needed, and after I figured out how to manually register the reader, it worked fine.

> On Tuesday, April 11, 2017, KARR, DAVID <dk...@att.com> wrote:
> 
> > I posted this on SO
> > (https://urldefense.proofpoint.com/v2/url?u=http-3A__stackoverflow.com
> > _&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-xy2uk0vYF_EA&m=t5H1VVQ
> > JcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=RZgen5CwkecIc_3aDZCfdv4MggcCt_7
> > MwGPZWv3gLXw&e=
> > questions/43333855/using-cxf-clientbuilder-how-to-unmarshal-post-respo
> > nse- parameters-into-java-cla), but some people might not see it, but
> > might see it here, so I'm asking here also.
> >
> > To put it another way, I have the following code as a base:
> > -------------------
> >         public CodeAndMessage validateToken(String token) {
> >                 WebTarget       target  =
> client.target(getHostPort()).
> > path(getPath());
> >                 Builder request = target.request(MediaType.
> > APPLICATION_JSON_TYPE);
> >                 Form  form    = new Form();
> >                 form.param("TokenID", token);
> >                 Form  responseForm    =
> request.post(Entity.entity(form,
> > MediaType.APPLICATION_FORM_URLENCODED_TYPE), Form.class);
> >                 System.out.println("responseForm[" + responseForm + "]
> > map[" + responseForm.asMap() + "]");
> >                 return new CodeAndMessage().
> >                             errorCode(responseForm.asMap()
> > .getFirst("errorCode")).
> >
> errorMessage(responseForm.asMap().getFirst("
> > errorMessage"));
> >         }
> > ----------------
> >
> > Where "CodeAndMessage" is a simple POJO with two fields.
> >
> > This works, but I was wondering if there is a cleaner way to do this,
> > to avoid having to use the Form response and getting the map values.
> >
> 
> 
> --
> Sergey Beryozkin
> 
> https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__sberyozkin.blogspot.com&d=DwIBaQ&c=LFYZ-
> o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=SjdRkGUeJQe
> WcTlvRwImNayrGo841bzo52GGjT61Tfs&e=
> Talend - https://urldefense.proofpoint.com/v2/url?u=http-
> 3A__www.talend.com&d=DwIBaQ&c=LFYZ-o9_HUMeMTSQicvjIg&r=OsTemSXEn-
> xy2uk0vYF_EA&m=t5H1VVQJcPekInd0PePjrjSEVSkWQBuWPEFhDlrQ62A&s=JohgjpbXgXz
> hcMmAnCzGU135vYCDCXC6eiNQlciJx8U&e=

Re: Better way to get post response parameters into a POJO?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi, well, if the response data are in the form format then you need to read
it as a form, but perhaps a cleaner way is to create MessageBodyReader for
CodeAndMessage, which can convert a form response,
Cheers Sergey

On Tuesday, April 11, 2017, KARR, DAVID <dk...@att.com> wrote:

> I posted this on SO (http://stackoverflow.com/
> questions/43333855/using-cxf-clientbuilder-how-to-unmarshal-post-response-
> parameters-into-java-cla), but some people might not see it, but might
> see it here, so I'm asking here also.
>
> To put it another way, I have the following code as a base:
> -------------------
>         public CodeAndMessage validateToken(String token) {
>                 WebTarget       target  = client.target(getHostPort()).
> path(getPath());
>                 Builder request = target.request(MediaType.
> APPLICATION_JSON_TYPE);
>                 Form  form    = new Form();
>                 form.param("TokenID", token);
>                 Form  responseForm    = request.post(Entity.entity(form,
> MediaType.APPLICATION_FORM_URLENCODED_TYPE), Form.class);
>                 System.out.println("responseForm[" + responseForm + "]
> map[" + responseForm.asMap() + "]");
>                 return new CodeAndMessage().
>                             errorCode(responseForm.asMap()
> .getFirst("errorCode")).
>                             errorMessage(responseForm.asMap().getFirst("
> errorMessage"));
>         }
> ----------------
>
> Where "CodeAndMessage" is a simple POJO with two fields.
>
> This works, but I was wondering if there is a cleaner way to do this, to
> avoid having to use the Form response and getting the map values.
>


-- 
Sergey Beryozkin

http://sberyozkin.blogspot.com
Talend - http://www.talend.com