【问题标题】:Returning data from rest web service with NO parameters in uri从 uri 中没有参数的 rest web 服务返回数据
【发布时间】:2011-11-08 01:00:56
【问题描述】:

从 REST Web 服务返回字符串时遇到问题:

[WebInvoke(Method = "PUT", UriTemplate = "/Filters", 
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
string CreateFilter(Filter InputFilter);

如您所见,URL 没有任何参数,所有数据都通过流传递到 URL。返回的字符串基于发送到 URL 的数据。

当我使用 .net 测试应用程序时,响应是正确的(基于输入)。

当我使用 Java servlet 时,服务会在调用 HttpURLRequest 的 GetInputStream() 时返回 400 错误。

Java servlet 似乎对 URL 进行了第二次调用并期望得到响应(响应不存在,因为它基于输入)。我通过将返回更改为常量(因此不基于输入)来测试这一点。当我这样做时,我的 GetInputStream() 方法有效。

servlet 代码:

OutputStream out = connection.getOutputStream();
out.write(jsonValue.getBytes());
out.close();

String response = "";


BufferedReader readResponse = 
new BufferedReader(new InputStreamReader(connection.getInputStream()));

response = readResponse.readLine();


return response;

注意:其他方法对带有参数的 URL 使用相同的代码,并返回正确的数据(添加证据表明 servlet 在我的问题方法中对 URL 进行了第二次调用。)还有 out.close()不是问题。

【问题讨论】:

    标签: java rest servlets return inputstream


    【解决方案1】:

    解决了。这最终成为 Servlet 发送的 Json 中的一个问题。当 Web 服务尝试使用某一特定数据时,它会抛出错误,并且不会生成所需的返回字符串。

    [{"Key":"1","Value":"one"},{"Key":"2","Value":"two"},{"Key":"3","Value":"three"}]
    

    C# 表示 ^^

    {"3":"three","2":"two","1":"one"}
    

    Java 表示 ^^

    字典 vs HashMap >

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多