将数据发布到数据库时出错204

时间:2013-10-24 15:57:01

标签: java rest jersey netbeans-6.9

我是Restful网络服务的新手。我想通过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;
}

1 个答案:

答案 0 :(得分:1)

204不是错误代码。所有200系列代码都意味着成功。有关状态代码及其含义,请参阅http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

204表示“无内容”。服务器已完成请求,但不需要返回实体主体。

如果你得到204并且服务器端错误正在抛出堆栈跟踪,那么它应该返回500或类似的。如果是这种情况,您需要发布它们以获得更多帮助。