0

我需要一点帮助,我是在 java 中创建休息方法的新手,但我找到了它并创建了一个休息方法。我有一个包含不同方法的类。这是我的课

@Path("/WebServices ")
public class WebServices {
@POST
@Path("/SourceCreateService")
@Consumes("multipart/related")
@Produces("text/plain")
public String sourceCreateService(@QueryParam("sourceTiltle") String sourceTiltle, @QueryParam("xml") String xml) {
return "name";
}
}

And now i have to access this method in another class,I can use this code to access this method in this class,

try{
URL url = new URL("http://localhost:8080/web/WebServices/SourceCreateService?sourceTiltle=sdds&xml="XML");

URLConnection conn = url.openConnection();
conn.setDoOutput(true); // Triggers POST.
// conn.setDoInput(true);
conn.setRequestProperty("Accept-Charset", charset);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

BufferedWriter out =
new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
out.write("username=name\r\n");
out.flush();
out.close();
BufferedReader in =
new BufferedReader( new InputStreamReader( conn.getInputStream() ) );

}catch(IOException e){
system.out.println(""+e);

}

when  i call this method i got this error,

java.io.IOException: Server returned HTTP response code: 505 for URL:

I put debugpoint in web service method also,but it not come to that method,it directly throws exception here,so my question is that my webservice methog is right and kindly tell me what is the wrong in my code and my URL

web.xml中是否有任何配置

4

1 回答 1

0

由于您要发布表单 URL 编码数据,因此您必须使用 @FormParam 而不是 @QueryParam 注释方法参数

@Path("/WebServices ")
public class WebServices {
    @POST
    @Path("/SourceCreateService")
    @Consumes("multipart/related") @Produces("text/plain")
    public String sourceCreateService(@FormParam("sourceTiltle") String sourceTiltle,     
                                      @QueryParam("xml") String xml) {
            return "name";
    }
}

看到这个问题/答案:jQuery not POSTing URL arguments to Jersey service?

于 2013-05-30T05:10:55.613 回答