2012-03-13 62 views
6

嗨,我有我想學習寧靜services.I創建使用JAX-RS,其如下所示讀取HTTP POST頭

@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(); 
} 
} 

我用下面的測試客戶端來測試服務的Web服務故障

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(); 
} 
} 

我想在服務器端頭使用的密碼,並做我對着database.The問題查找是我怎麼在服務器讀取頭。

回答

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(); 
} 

}

10

我不是很熟悉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(); 
    } 
} 

希望這有助於!

+0

感謝MRDC的暗示! – 2012-03-14 00:48:19