1

Restful Web サービスは初めてです。Jersey クライアント経由でサーバーにアクセスしたいと考えています。しかし、204 エラーが発生します。私がやろうとしているのは、クライアント エンドから ID 値を送信し、適切な名前を取得することです。私はブラウザでこれを行いましたが、完全に機能します。誰でもここでエラーを見つけることができますか?

これがクライアント エンドです。

public Link(String param, String val) throws ClientProtocolException, IOException {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());

    //System.out.println(service.path("main").path("db").accept(MediaType.TEXT_PLAIN).get(String.class));


    MultivaluedMap pathParams = new MultivaluedMapImpl();
    pathParams.add(param, val);
    System.out.println(param+":" + val);

    ClientResponse response = service.path("main").path("ds").type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, pathParams);
    System.out.println(response.toString());
    System.out.println(response.getEntity(String.class));



}

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/WebApp/resources/").build();
}

これはサーバーエンドです

@Path("/main")
public class WebService {
 @Path("/ds")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String returnData(@PathParam("id") String id_no) throws Exception{
    PreparedStatement query = null;
    String myString = null;
    java.sql.Connection conn = null;

    System.out.println(id_no);

    try{
        conn= Connection.createCon();
        query = conn.prepareStatement("select name as ds_name from student where id='" + id_no + "'");
        ResultSet firstweb_rs = query.executeQuery();

        while(firstweb_rs.next()){
            myString = firstweb_rs.getString("ds_name");
        }
        query.close();


    } catch(Exception e){
        e.printStackTrace();
    }
    finally{
        if(conn!=null) conn.close();

    }
    return myString;
}
4

1 に答える 1

1

204 はエラー コードではありません。200 番台のコードはすべて成功を意味します。ステータス コードとその意味については、http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.htmlを参照してください。

204は「コンテンツなし」を意味します。サーバーはリクエストを実行しましたが、エンティティ ボディを返す必要はありません。

204 を取得していて、スタック トレースをスローしているサーバー側のエラーがある場合は、500 などを返す必要があります。その場合は、さらにヘルプを得るためにそれらを投稿する必要があります。

于 2013-10-24T17:05:36.413 に答える