I am retreiving values from multiple RESTful Web Service methods. In this case two methods interfere with one another, due to a problem with the 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 I try to call the createPerson() method using the following code, my Glassfish Server will result in "@GET /person/ the name I'm trying to create a person on". Which means the @GET method is called, even though I did not send a {name} parameter (as you can see in the 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 this is asking a lot of digging in my code, but what am I doing wrong in this case?
UPDATE
Because createPerson is a void, I do not handle a connection.getInputStream(). This actually seems to result in the request not being handled by my Service.
But the actual request is sent on the connection.getOutputStream(), right?
UPDATE 2
The RequestMethod does work, as long as I handle a method with a return value and thus connection.getOutputStream(). When I try calling a void and thus not handling connection.getOutputStream(), the Service will not receive any request.
You should set the "Content-Type" instead of "Accept" header. Content-Type specifies the media type sent to the recipient, while Accept is about the media type accepted by the client. More details on headers are here.
Here is the Java client:
Here is the same using curl:
, where "person" is a file containing the xml: