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; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!