httpUrlCon.getInputStream()在调用Restful API时导致IOException

时间:2013-07-01 07:03:36

标签: java web-services rest

我使用HttpURLConnection调用了Restful Web Service.Below是我的代码的摘要:


休息图层

@Path("/ResourceName")
@Scope("request")
@SuppressWarnings("unchecked")
public class AlertResource {
     @POST
     @Path("/methodName")
     @Produces({ MediaType.APPLICATION_JSON })
     public Response methodName(
          @@HeaderParam(value = "xxxx") String param1){
      .....
     }

动作层

public static String callRestfulApp(String urlString, String methodType, 
                      Map<String,   String> parameter) throws Exception{

    InputStream inputStream = null;
    StringBuilder responseText = new StringBuilder();
    HttpURLConnection httpUrlCon = null;
    try {
        URL url;            
        StringBuilder urlData = new StringBuilder();
        urlData.append(urlString);
        url = new URL(urlData.toString());
        httpUrlCon = (HttpURLConnection) url.openConnection();
        httpUrlCon.setDoOutput(true);
        httpUrlCon.setRequestMethod(methodType);

        if (parameter != null) {
            // Set the parameters
            for (Map.Entry<String, String> entry : parameter.entrySet()) {
                httpUrlCon.setRequestProperty(entry.getKey(),entry.getValue());
            }
        }           

        inputStream = httpUrlCon.getInputStream();
        .....

我使用以下行来调用它,

 parameters.put("xxxx",
          gson.toJson(someBean));

 respons = CommonWebUtils.callRestfulApp(prop.getPropertyValue("RestURI")                     + "ResourceName/methodName",
   "POST", parameters);

上面代码的问题是,我的“someBean”包含大量数据,所以因为这个; httpUrlCon.getInputStream()导致IOException。如果我传递的数据较少,那么它的工作正常。我试过dirrerent参数传递技术(查询,矩阵),但没有那些帮助我。所以,如何传递一个巨大的数据?

异常追踪:

    java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:8091/Restful/ResourceName/methodName
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
        at org.razorsight.alert.web.common.CommonWebUtils.callRestfulApp1(CommonWebUtils.java:149)
        at org.razorsight.alert.web.action.KpiAction.addKpiRule(KpiAction.java:245)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
        at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
        at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
        at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
        at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
        at javax.faces.component.UICommand.broadcast(UICommand.java:315)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)

1 个答案:

答案 0 :(得分:0)

最后我发现了问题及其解决方案。

我正在使用HeaderParam将我的参数传递给web服务。每个服务器都有一个支持的头的max.size配置。我正在使用Tomcat 6.So,default it suuport max of 8KB。< / p>

但是,在我的情况下;参数的总大小超过8KB。因此,它无法获得Web服务方法的输入流。为了解决这个问题,我只修改了它支持的默认httpheader大小{使用server.xml的Tomcat {1}} 属性。

maxHttpHeaderSize