Appache HTTPコンポーネントライブラリは、通常のWebサイトとの通信には正常に機能しているようですが、Webサービスに接続できません。
私は、Jerseyフレームワークを使用してWebサービス-RESTと通信するJavaDesktopアプリケーション内で実行するコンポーネントを構築しています。Webサービスが実行されており、WebブラウザーおよびNetBeansのTest-Web-Service関数と通信できます。
通常のWebページ、またはマシンで実行されているGlassfish Webサーバーのデフォルトページから読み込もうとすると、クライアントコードは正常に機能します。クライアントプログラム(以下のコード)を使用してWebサービスにアクセスしようとすると、Webサーバーから404 Not Foundエラーが発生するのはなぜですか?
import java.io.*;
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.*; //HttpHead, HttpPut, HttpGet, etc...
import org.apache.http.util.EntityUtils;
public class ApacheClientDemo {
public static void demo() throws IOException {
String uri = "http://localhost:8080/Proctorest/resources/helloWorld";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
System.out.println();
System.out.println(EntityUtils.toString(entity));
}
public static void main(String[] args) {
try {
demo();
}
catch(IOException ioe) {
System.out.println(ioe);
}
}
}
オンラインの通常のWebサイト、またはローカルでホストされている非Webサービスサイトにアクセスすると、次のように出力されます。
HTTP/1.1 200 OK
Webページのコンテンツが続きます。
上記のコードは私に与えます
HTTP/1.1 404 Not Found
編集:以下はWebサービスのコードです。Webサービスコードは、Netbeansサンプルプロジェクトから生成されました。
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package helloworld;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
/**
* REST Web Service
*
* @author mkuchtiak
*/
@Stateless
@Path("/helloWorld")
public class HelloWorldResource {
@EJB
private NameStorageBean nameStorage;
/**
* Retrieves representation of an instance of helloworld.HelloWorldResource
* @return an instance of java.lang.String
*/
@GET
@Produces("text/html")
public String getXml() {
return "<html><body><h1>Hello "+nameStorage.getName()+"!</h1></body></html>";
}
/**
* PUT method for updating an instance of HelloWorldResource
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes("text/plain")
public void putXml(String content) {
nameStorage.setName(content);
}
}
/////////// NameStorageBean .java
import javax.ejb.Singleton;
/** Singleton session bean used to store the name parameter for "/helloWorld" resource
*
* @author mkuchtiak
*/
@Singleton
public class NameStorageBean {
// name field
private String name = "World";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}