gpt4 book ai didi

java - 将数据 POST 到数据库时出现错误 204

转载 作者:太空宇宙 更新时间:2023-11-04 07:19:04 27 4
gpt4 key购买 nike

我是 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;
}

最佳答案

204 不是错误代码。所有 200 系列代码均表示成功。请参阅http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html了解状态代码及其含义。

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

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

关于java - 将数据 POST 到数据库时出现错误 204,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19570831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com