RESTful Webservice无法正确处理Request Method [英] RESTful Webservice does not handle Request Method properly
问题描述
我正在从多个RESTful Web Service方法中检索值。
在这种情况下,由于请求方法存在问题,两种方法会相互干扰。
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);
}
当我尝试使用以下代码调用createPerson()方法时,我的Glassfish服务器将导致@GET / person / 我试图在上创建一个人的名称。这意味着@GET方法被调用,即使我没有发送{name}参数(如代码中所示)。
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());
我知道这需要大量挖掘我的代码,但我在这方面做错了什么case?
I know this is asking a lot of digging in my code, but what am I doing wrong in this case?
UPDATE
因为createPerson是一个空白,我无法处理connection.getInputStream()。
这实际上似乎导致我的服务没有处理请求。
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.
但实际请求是在connection.getOutputStream()上发送的,对吧?
But the actual request is sent on the connection.getOutputStream(), right?
更新2
RequestMethod确实有效,只要我处理一个具有返回值的方法,因此是connection.getOutputStream()。当我尝试调用void而不处理connection.getOutputStream()时,服务将不会收到任何请求。
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.
推荐答案
你应设置Content-Type而不是Accept标题。 Content-Type指定发送给收件人的媒体类型,而Accept是关于客户端接受的媒体类型。有关标题的更多详细信息,请此处。
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.
这是Java客户端:
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();
}
以下是使用curl的相同内容:
Here is the same using curl:
curl -X POST -d @person --header "Content-Type:application/xml" http://localhost:8080/RESTfulService/service/person
,其中person是包含xml的文件:
, where "person" is a file containing the xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><person><name>1234567</name></person>
这篇关于RESTful Webservice无法正确处理Request Method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!