【问题标题】:How to access post java method in client如何在客户端访问 post java 方法
【发布时间】:2013-05-30 05:02:03
【问题描述】:

我需要一点帮助,我是在 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中有没有配置

【问题讨论】:

    标签: java rest jax-rs restful-url


    【解决方案1】:

    由于您发布的是表单 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?

    【讨论】:

    • 感谢您的回答,我也尝试使用 FormParam,但也无法正常工作,如果我使用 formparam 方式,我的 URL 怎么样?
    • web.xml中是否有任何配置可以访问其余方法,请帮助我
    • 您必须区分 URL 参数和表单 URL 编码样式的 POST 内容。我的意思是:
      - /SourceCreateService?xml=someXML 是一个 @QueryParam - 但是如果您查看发布到服务器的内容(发布的内容),您会看到如下内容: sourceTitle=titlevalue 这第二个是 @FormParam
    猜你喜欢
    • 2015-11-22
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    相关资源
    最近更新 更多