infection...

jersey client 를 이용한 rest api 호출 본문

Develop/JAVA

jersey client 를 이용한 rest api 호출

돗거노인 2015. 2. 4. 13:39
pom.xml 에 jersy client 추가
        
			com.sun.jersey
			jersey-client
			${jersey.version}
	
maven install 후
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

Client client = Client.create();
			
			MultivaluedMap queryParams = new MultivaluedMapImpl();
		    queryParams.add("grant_type", "client_credentials");
			 
			WebResource webResource = client
			   .resource("url");
			
			ClientResponse response = null;
			response = webResource.queryParams(queryParams)
					.header("Content-Type", "application/json;charset=UTF-8")
                                       .get(ClientResponse.class);
			
	 
			if (response.getStatus() != 200) {
			   throw new RuntimeException("Failed : HTTP error code : "
				+ response.getStatus());
			}
	 
			String output = response.getEntity(String.class);
			System.out.println(output);