Question

I am new to Restful web services. I want to access the Server via a Jersey Client. But I am getting a 204 error. What i am trying to do is submit an id values through the client end and retrieve the appropriate name. I have done this through browser and it works perfectly. Can anyone please find the error here?

This is the Client End.

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();
}

This is the Server End

@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;
}
Was it helpful?

Solution

204 is not an error code. All of the 200 series codes mean Success. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for the status codes and their meanings.

204 means "No Content". The server has fulfilled the request but does not need to return an entity-body.

If you're getting 204 and there's a server side error that's throwing stack traces then it should be returning a 500 or similar. If that's the case you'll need to post them up to get more help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top