Hi Tôi đang gặp khó khăn tôi đang cố gắng tìm hiểu services.I yên tĩnh tạo ra một dịch vụ web bằng jax-rs được hiển thị dưới đâyđọc http post tiêu đề
@Path("/users")
public class Welcome {
@POST
@Consumes("text/xml")
@Produces("text/xml")
public Response welcome(String incomingXML){
return Response.status(200).entity("timestamp : " + incomingXML).build();
}
}
tôi sử dụng cho khách hàng thử nghiệm sau để kiểm tra dịch vụ
public class TestService {
public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException {
ClientConfig config = new DefaultClientConfig();
Client client=Client.create(config);
WebResource service=client.resource(getBaseURI());
String urlString = "http://localhost:8080/JaXRSDemo/rest/users";
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// set up url connection to get retrieve information back
con.setRequestMethod("POST");
con.setDoInput(true);
// stuff the Authorization request header
byte[] encodedPassword = (userName + ":" + password).getBytes();
con.setRequestProperty("Authorization",encodedPassword.toString());
Customer customer=new Customer();
customer.setName("noobstre");
customer.setPin(123455);
ClientResponse response=service.path("rest").path("users").type(MediaType.APPLICATION_XML).post(ClientResponse.class,customer);
System.out.println(" response " + response.getEntity(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/JaXRSDemo").build();
}
}
Tôi muốn sử dụng mật khẩu trong tiêu đề ở phía máy chủ và tìm kiếm cơ sở dữ liệu. Vấn đề tôi đang gặp là làm cách nào để đọc tiêu đề tại máy chủ.
nhờ mrdc cho gợi ý! –