java - RESTful Webservice does not handle Request Method properly -


i retreiving values multiple restful web service methods. in case 2 methods interfere 1 another, due problem request method.

@get @path("/person/{name}") @produces("application/xml") public person getperson(@pathparam("name") string name) {     system.out.println("@get /person/" + name);     return people.byname(name); }  @post @path("/person") @consumes("application/xml") public void createperson(person person) {     system.out.println("@post /person");     system.out.println(person.getid() + ": " + person.getname());     people.add(person); } 

as try call createperson() method using following code, glassfish server result in "@get /person/ the name i'm trying create person on". means @get method called, though did not send {name} parameter (as can see in code).

url url = new url("http://localhost:8080/restfulservice/service/person"); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("post"); connection.setrequestproperty("accept", "application/xml"); connection.setdooutput(true); marshaller.marshal(person, connection.getoutputstream()); 

i know asking lot of digging in code, doing wrong in case?

update

because createperson void, not handle connection.getinputstream(). seems result in request not being handled service.

but actual request sent on connection.getoutputstream(), right?

update 2

the requestmethod work, long handle method return value , connection.getoutputstream(). when try calling void , not handling connection.getoutputstream(), service not receive request.

you should set "content-type" instead of "accept" header. content-type specifies media type sent recipient, while accept media type accepted client. more details on headers here.

here java client:

public static void main(string[] args) throws exception {     string data = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><person><name>1234567</name></person>";     url url = new url("http://localhost:8080/restfulservice/service/person");     httpurlconnection connection = (httpurlconnection) url.openconnection();     connection.setrequestmethod("post");     connection.setrequestproperty("content-type", "application/xml");      connection.setdooutput(true);     connection.setdoinput(true);              outputstreamwriter wr = new outputstreamwriter(connection.getoutputstream());     wr.write(data);     wr.flush();      // response     bufferedreader rd = new bufferedreader(new inputstreamreader(connection.getinputstream()));     wr.close();     rd.close();      connection.disconnect(); } 

here same using curl:

curl -x post -d @person --header "content-type:application/xml" http://localhost:8080/restfulservice/service/person 

, "person" file containing xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?><person><name>1234567</name></person> 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -