1

I have developed a webservice following the below link, however I am unable to get the request parameters from the POST request body.

http://info.appdirect.com/blog/how-to-easily-build-rest-web-services-with-java-spring-and-apache-cxf

I use the Soap UI to invoke the service, with "Post QueryString in Message Body" option checked. So far, I have tried below options, but none seem to work:

  • Tried MultiValued Map, but the map is always empty.

    @POST
    @Path("/getpostfile/{fileName}")
    @Produces("application/pdf")
    @Consumes("application/x-www-form-urlencoded")
    public Response getPostFile(MultivaluedMap form){...}
    
  • Tried @FormParam() & @QueryParam as well, but still the params are null in webservice method.

  • Tried creating a POJO bean with @XmlRootElement annotation, however this time I get an exception saying "SEVERE: No message body reader has been found for class com.wcc.LoginInfo, ContentType: application/x-www-form-urlencoded". LoginInfo is my bean class with two parameters:

    @XmlRootElement
    public class LoginInfo {
        String username;
        String password;
        ....
    

Below is the service method:

    @POST
    @Path("/getpostfile/{fileName}")
    @Produces("application/pdf")
    @Consumes("application/x-www-form-urlencoded")
    public Response getPostFile(LoginInfo logininfo){...}

Below is my cxf-beans.xml file:

    <bean .....>
    <jaxrs:server id="wccservice" address="/">
       <jaxrs:serviceBeans>
           <ref bean="wccdocumentservice" />
       </jaxrs:serviceBeans>
       <jaxrs:providers>
            <ref bean="formUrlEncodeProvider" />
       </jaxrs:providers>
 </jaxrs:server>


 <bean id="wccdocumentservice" class="com.wcc.WCCDocumentServiceImpl" />
 <bean id="formUrlEncodeProvider" class="org.apache.cxf.jaxrs.provider.FormEncodingProvider" />
 <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
    <property name="marshallAsJaxbElement" value="true" />
 </bean>

Any ideas on how to retrieve the POST parameters in request body, in the Webservice method?

Pankaj
  • 229
  • 3
  • 7

1 Answers1

2

Guess I was dwelling too much into CXF, missed the simple approach. We can get the Post request parameters in CXF service similar to the way we get the parameters in a Servlet.

For Query Parameters in POST request:

    @POST
    @Path("/getpostfile/{fileName}")
    @Produces("application/pdf")
    @Consumes("application/x-www-form-urlencoded")
    public Response getPostFile(@PathParam("fileName") String fileName, @Context HttpServletRequest request) 
    {
     System.out.println("id is : " + request.getParameter("id")+", password is : " + request.getParameter("password"));

and for Parameters in POST message body, you may use getBody() method from below link:

Getting request payload from POST request in Java servlet

Hope that helps someone!

Community
  • 1
  • 1
Pankaj
  • 229
  • 3
  • 7