You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by deniak <de...@gmail.com> on 2008/06/27 00:37:26 UTC

CXF Rest Post Method

Hi all

I just start with CXF and i have a little problem with the post method.
Here's my code:

- myClass.java
@Path("test")
public class myClass{
	@POST
	@Path("/newtest")
	public String myMethod(String myParam){
              return myParam;
        }
}
 

To test my webservice, i execute this code:

 
public static void main(String args[]) {
     PostMethod post = new PostMethod("http://localhost:8080/test/newtest");
     HttpClient httpclient = new HttpClient();
     try {
            int result = httpclient.executeMethod(post);
     }finally {
            post.releaseConnection();
     }
}
 

My question is how do I specify the parameter "myParam" in the main method?

Thanks for your help
-- 
View this message in context: http://www.nabble.com/CXF-Rest-Post-Method-tp18144858p18144858.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: CXF Rest Post Method

Posted by Arul Dhesiaseelan <ar...@fluxcorp.com>.
Here you go.

@Path("/customers")
public class Customers {
 @POST
 @ConsumeMime(MediaType.TEXT_PLAIN)
 public Response postString(String input) {
    //process the input
 }
}
If you post to this URL http://localhost:8080/customers, this method 
would be invoked.

-Arul

deniak wrote:
> Can you give me an example with plain text request??
>
> Thank you
>
>
> Arul Dhesiaseelan wrote:
>   
>> It depends on what your resource can consume. You can post plain text or 
>> json or xml request too.
>>
>> The below sample allows you to post an XML request to your resource.
>>
>> @Path("/customers")
>> public class Customers {
>>   @POST
>>   @Path("add")
>>   @ConsumeMime(MediaType.APPLICATION_XML)
>>   public Response addCusomter(Customer customer) {
>>       //process the customer pojo in memory or db
>>   }
>> }
>>
>>
>> You can send in a customer XML in the HTTPClient post operation as
>>     File input = new File("customer.xml");
>>     PostMethod post = new
>> PostMethod("http://localhost:8080/customers/add");
>>     post.addRequestHeader("Accept" , "application/xml");
>>     RequestEntity entity = new FileRequestEntity(input,
>> "application/xml");
>>     post.setRequestEntity(entity);
>>    
>>     httpclient.executeMethod(post);
>>
>> HTH.
>>
>> -Arul
>>
>> deniak wrote:
>>     
>>> Hi all
>>>
>>> I just start with CXF and i have a little problem with the post method.
>>> Here's my code:
>>>
>>> - myClass.java
>>> @Path("test")
>>> public class myClass{
>>> 	@POST
>>> 	@Path("/newtest")
>>> 	public String myMethod(String myParam){
>>>               return myParam;
>>>         }
>>> }
>>>  
>>>
>>> To test my webservice, i execute this code:
>>>
>>>  
>>> public static void main(String args[]) {
>>>      PostMethod post = new
>>> PostMethod("http://localhost:8080/test/newtest");
>>>      HttpClient httpclient = new HttpClient();
>>>      try {
>>>             int result = httpclient.executeMethod(post);
>>>      }finally {
>>>             post.releaseConnection();
>>>      }
>>> }
>>>  
>>>
>>> My question is how do I specify the parameter "myParam" in the main
>>> method?
>>>
>>> Thanks for your help
>>>   
>>>       
>>
>>     
>
>   


Re: CXF Rest Post Method

Posted by deniak <de...@gmail.com>.
Can you give me an example with plain text request??

Thank you


Arul Dhesiaseelan wrote:
> 
> It depends on what your resource can consume. You can post plain text or 
> json or xml request too.
> 
> The below sample allows you to post an XML request to your resource.
> 
> @Path("/customers")
> public class Customers {
>   @POST
>   @Path("add")
>   @ConsumeMime(MediaType.APPLICATION_XML)
>   public Response addCusomter(Customer customer) {
>       //process the customer pojo in memory or db
>   }
> }
> 
> 
> You can send in a customer XML in the HTTPClient post operation as
>     File input = new File("customer.xml");
>     PostMethod post = new
> PostMethod("http://localhost:8080/customers/add");
>     post.addRequestHeader("Accept" , "application/xml");
>     RequestEntity entity = new FileRequestEntity(input,
> "application/xml");
>     post.setRequestEntity(entity);
>    
>     httpclient.executeMethod(post);
> 
> HTH.
> 
> -Arul
> 
> deniak wrote:
>> Hi all
>>
>> I just start with CXF and i have a little problem with the post method.
>> Here's my code:
>>
>> - myClass.java
>> @Path("test")
>> public class myClass{
>> 	@POST
>> 	@Path("/newtest")
>> 	public String myMethod(String myParam){
>>               return myParam;
>>         }
>> }
>>  
>>
>> To test my webservice, i execute this code:
>>
>>  
>> public static void main(String args[]) {
>>      PostMethod post = new
>> PostMethod("http://localhost:8080/test/newtest");
>>      HttpClient httpclient = new HttpClient();
>>      try {
>>             int result = httpclient.executeMethod(post);
>>      }finally {
>>             post.releaseConnection();
>>      }
>> }
>>  
>>
>> My question is how do I specify the parameter "myParam" in the main
>> method?
>>
>> Thanks for your help
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/CXF-Rest-Post-Method-tp18144858p18148713.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: CXF Rest Post Method

Posted by Arul Dhesiaseelan <ar...@fluxcorp.com>.
It depends on what your resource can consume. You can post plain text or 
json or xml request too.

The below sample allows you to post an XML request to your resource.

@Path("/customers")
public class Customers {
  @POST
  @Path("add")
  @ConsumeMime(MediaType.APPLICATION_XML)
  public Response addCusomter(Customer customer) {
      //process the customer pojo in memory or db
  }
}


You can send in a customer XML in the HTTPClient post operation as
    File input = new File("customer.xml");
    PostMethod post = new PostMethod("http://localhost:8080/customers/add");
    post.addRequestHeader("Accept" , "application/xml");
    RequestEntity entity = new FileRequestEntity(input, "application/xml");
    post.setRequestEntity(entity);
   
    httpclient.executeMethod(post);

HTH.

-Arul

deniak wrote:
> Hi all
>
> I just start with CXF and i have a little problem with the post method.
> Here's my code:
>
> - myClass.java
> @Path("test")
> public class myClass{
> 	@POST
> 	@Path("/newtest")
> 	public String myMethod(String myParam){
>               return myParam;
>         }
> }
>  
>
> To test my webservice, i execute this code:
>
>  
> public static void main(String args[]) {
>      PostMethod post = new PostMethod("http://localhost:8080/test/newtest");
>      HttpClient httpclient = new HttpClient();
>      try {
>             int result = httpclient.executeMethod(post);
>      }finally {
>             post.releaseConnection();
>      }
> }
>  
>
> My question is how do I specify the parameter "myParam" in the main method?
>
> Thanks for your help
>