有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

向数据库发布数据时出现java错误204

我不熟悉Restful web服务。我想通过Jersey客户端访问服务器。但我有个错误。我试图做的是通过客户端提交一个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) 个答案

  1. # 1 楼答案

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

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

    如果得到204,并且服务器端出现了抛出堆栈跟踪的错误,那么它应该返回500或类似的值。如果是这样的话,你需要把它们贴上去以获得更多帮助