httpclient 认证方式访问http api/resutful api并获取json结果

最近,因公司线上环境rabbitmq经常发生堆积严重的现象,于是跟运维组讨论,帮助开发个集中监控所有rabbitmq服务器运行情况的应用,需要通过java访问rabbitmq暴露的http api并接收json结果,并将下划线转换为驼峰式,其中包括认证。

httpclient 认证方式访问http api/resutful api并获取json结果的相关代码如下:

package com.medsoft.client.test.action;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)  //在jackson 2.x中,设置忽略无法映射的属性通过设置在bean上的注解来实现,在jackson 1.x中,则是通过DeserializationConfig.Feature来设置,2.x已经没有这个接口了
public class Queue {
    private String name;
    private String node;
    
    private long messages;
    @JsonProperty("messages_ready")
    private long messagesReady;
    @JsonProperty("messages_unacknowledged")
    private long messagesUnacknowledged;
    @JsonProperty("message_bytes_ram")
    private long messageBytesRam;
    @JsonProperty("message_bytes")
    private long messageBytes;
    private long memory;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNode() {
        return node;
    }
    public void setNode(String node) {
        this.node = node;
    }
    
    public long getMessageBytesRam() {
        return messageBytesRam;
    }
    
    public void setMessageBytesRam(long messageBytesRam) {
        this.messageBytesRam = messageBytesRam;
    }
    
    public long getMemory() {
        return memory;
    }
    public void setMemory(long memory) {
        this.memory = memory;
    }
    public long getMessages() {
        return messages;
    }
    public void setMessages(long messages) {
        this.messages = messages;
    }
}

 

	public static void main(String[] args) {
		httpGet("http://localhost:15672/api/queues/%2f/" + "test-api");
	}
	
	public static void httpGet(String url){
		//设置认证信息
		HttpHost targetHost = new HttpHost("localhost", 15672, "http");
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(AuthScope.ANY, 
		  new UsernamePasswordCredentials("guest", "guest"));

		AuthCache authCache = new BasicAuthCache();
		authCache.put(targetHost, new BasicScheme());

		// Add AuthCache to the execution context
		final HttpClientContext context = HttpClientContext.create();
		context.setCredentialsProvider(credsProvider);
		context.setAuthCache(authCache);
		
		try  
		{  
		    HttpClient client = HttpClientBuilder.create().build();
		    HttpResponse response = client.execute(new HttpGet(url), context);

		    //请求成功  
		    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)  
		    {  
		        //取得返回的字符串  
		        String strResult = EntityUtils.toString(response.getEntity());  
		        System.out.println(strResult);
		        //Queue queue = JsonUtils.json2Object(strResult, Queue.class);
		        ObjectMapper objectMapper = new ObjectMapper();
		        objectMapper.configure(MapperFeature.USE_ANNOTATIONS, true);  --默认情况下,注解不会启用,需要显示启用
		        objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector());  --默认情况下没有注解解析器,需要设置
		        Queue queue = objectMapper.readValue(strResult, Queue.class);
		        System.out.println(JsonUtils.toJson(queue));
		    }  
		    else  
		    {  
		       System.out.print(response.toString());
		    }  
		}  
		catch (ParseException | IOException e1)  
		{  
		    e1.printStackTrace();
		}
    }

  

posted @ 2016-10-14 10:22  zhjh256  阅读(466)  评论(0编辑  收藏  举报