1
我是Restful Web服务的新手。我想通过Jersey客户端访问服务器。但我得到了204错误。我想要做的是通过客户端提交一个id值并检索相应的名称。我通过浏览器完成了这一工作,并且完美地工作。任何人都可以在这里找到错误?将数据发布到数据库时发生错误204
这是客户端。
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;
}