Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send java object to a rest WebService

I'm searching a nice way to send a java object to my rest web service.
It's possible or not ?


For sample I wan't to send an "User" Object to my rest :

public Class User{
    private String name;
    private String surname;

    public getName(){
     return name;
    }

    public setName(String name){

      [...]
 }

It's possible to generate AUTOMATICALY this kind of Rest ?
www.foo.com/createUser/name="foo"&surname="foo"

like image 276
Martin Magakian Avatar asked Jul 18 '09 14:07

Martin Magakian


People also ask

How do you consume a REST webservice in Java?

Just make an http request to the required URL with correct query string, or request body. For example you could use java. net. HttpURLConnection and then consume via connection.

Can we call REST API from Java?

You can definitely interact with RESTful web services by using URLConnection or HTTPClient to code HTTP requests. However, it's generally more desirable to use a library or framework which provides a simpler and more semantic API specifically designed for this purpose.


3 Answers

I would consider using a JSON representation for this kind of Java objects. I prefer the Jersey implementation of JAX-RS and it has built-in support for JSON serialization over JAXB.

Hope this helps...

like image 115
Shimi Bandiel Avatar answered Oct 05 '22 23:10

Shimi Bandiel


Have a look at Restlet. The tutorial shows you how to get started.

Restlet allows you to use a number of representation formats, including XML and JSON.

like image 41
Rich Seller Avatar answered Oct 06 '22 01:10

Rich Seller


It's possible to generate AUTOMATICALY this kind of Rest ? www.foo.com/createUser/name="foo"&surname="foo"

That's NOT REST. That's RPC.

like image 23
aehlke Avatar answered Oct 06 '22 01:10

aehlke