RESTful Webservice does not handle Request Method

2020-07-27 03:43发布

问题:

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.

回答1:

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:

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();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    wr.close();
    rd.close();

    connection.disconnect();
}

Here is the same using curl:

curl -X POST -d @person --header "Content-Type:application/xml" http://localhost:8080/RESTfulService/service/person

, where "person" is a file containing the xml:

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