0

HttpURLConnection を使用して Restful Web Service を呼び出しました。コードの概要を以下に示します。


レストレイヤー

@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を引き起こしていることです。渡すデータが少ない場合、問題なく動作します。 (クエリ、マトリックス)、しかしそれらのどれも私を助けません.だから、巨大なデータを渡す方法は?

例外トレース:

    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)
4

1 に答える 1

0

最後に、問題とその解決策を見つけました。

パラメータを Web サービスに渡すために使用HeaderParamしています。すべてのサーバーには、サポートするヘッダーの max.size の構成があります。Tomcat 6.So,by を使用していdefault it suuport max of 8KBます。

しかし、私の場合、パラメーターの合計サイズは 8KB を超えています。そのため、Web サービス メソッドの入力ストリームを取得できません。この問題を解決するために、プロパティserver.xmlを使用して Tomcatでサポートされているデフォルトの httpheader サイズを変更するだけです。maxHttpHeaderSize.

<Connector connectionTimeout="20000" port="8091" protocol="HTTP/1.1" redirectPort="8443" maxHttpHeaderSize="15360"/>
于 2013-07-03T09:31:52.330 に答える