您好我遇到麻烦我正在尝试学习restful服务。我使用jax-rs创建了一个Web服务,如下所示
@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();
}
}
我使用以下测试客户端来测试服务
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();
}
}
我想在服务器端的标题中使用密码并对数据库进行查找。我面临的问题是如何读取服务器上的标题。
答案 0 :(得分:12)
我对Jax-RS不太熟悉,但您可以使用以下方法获取您正在寻找的标题信息:
1。)使用@HeaderParam
/**Server side******/
@Path("/users")
public class Welcome {
@POST
@Consumes("text/xml")
@Produces("text/xml")
public Response welcome(String incomingXML, @HeaderParam("Authorization") String authString)
{
//Use authString here
return Response.status(200).entity("timestamp : " + incomingXML).build();
}
}
2。)使用@Context
/**Server side******/
@Path("/users")
public class Welcome {
@POST
@Consumes("text/xml")
@Produces("text/xml")
public Response welcome(String incomingXML, @Context HttpHeaders headers)
{
//Get Authorization Header
String authString = headers.getRequestHeader("Authorization").get(0);
return Response.status(200).entity("timestamp : " + incomingXML).build();
}
}
希望这有帮助!
答案 1 :(得分:7)
我使用Jersey客户端
解决了这个问题//客户方
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
final String userName = "admin";
final String password = "admin";
String cred = userName + ":" + password;
WebResource service = client.resource(getBaseURI());
Customer customer = new Customer();
customer.setName("noob");
customer.setPin(123455);
ClientResponse response = service.path("rest").path("users")
.accept(MediaType.APPLICATION_XML)
.header("Authorization", cred)
.post(ClientResponse.class, customer);
System.out.println(" response " + response.getEntity(String.class));
在服务器端
@Path("/users")
public class Welcome {
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response welcome(String incomingXML, @Context HttpHeaders headers) {
String s = headers.getRequestHeaders().getFirst("authorization");
return Response.status(200).entity("timestamp : " + incomingXML + s)
.build();
}
}