controller层接收List类型的参数

ofeqx.com 方程式科技共享平台 外文资料好找好用。大数据有序化,
专业垂直领域体系的知识共享盘平台,发表您的创作与我们共产知识。

1 一定是post 提交
在这里插入图片描述
json 的参数
[{“type”:“20”,“authType”:“1,2,3”},{“type”:“21”,“authType”:“2,3”}]

2 一定要有@RequestBody

在这里插入图片描述

fastjson

json 转换成list

public RespVo insertAuthDir(@RequestBody String json){
    JSONObject jsonObject = JSONObject.parseObject(json);

    List<AuthDirTypeVo> list = JSON.parseArray(jsonObject.get("authTypeVoList").toString(),AuthDirTypeVo.class);

json 转换成对象

public RespVo updateDeployAndApprove(@RequestBody String json){

    JSONObject jsonObject = JSONObject.parseObject(json);
    String JSONStr = JSON.toJSONString(jsonObject.get("jrsStrategyDeploy"));
    JrsStrategyDeploy jrsStrategyDeploy = JSON.parseObject(JSONStr, JrsStrategyDeploy.class);

package com.jd.jmq.server.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**

  • Created by dingjun on 15-3-27.
    */
    public class HttpClientTest {
    private String authid = null;

    private String topic=“jmq_proxy”;
    private String app=“proxy”;
    private String password = “81FB129A”;
    /**
    private String topic=“jmq_test”;
    private String app=“jmq”;
    private String password = “0C2F4E21”;
    */
    private String[] servers = null;
    public void testAuth(){
    if(authid == null) {
    try {
    String str = httpclient(“http://192.168.149.144:8080/1.0/auth”, “{‘user’:'”+app+“‘,‘password’:’”+password+“‘,‘topic’:’”+topic+“‘,‘app’:’”+app+“'}”);
    JSONObject jsonObject = JSON.parseObject(str);
    JSONObject statusObj = jsonObject.getJSONObject(“status”);
    if (statusObj.getIntValue(“code”) == 0) {
    JSONObject result = jsonObject.getJSONObject(“result”);
    authid = result.getString(“authid”);
    JSONArray proxys = result.getJSONArray(“servers”);
    if(proxys.isEmpty()){
    throw new RuntimeException(“proxys is empty”);
    }else{
    servers = proxys.toArray(new String[proxys.size()]);
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    @Test
    public void testPut(){
    while(true) {
    String put = “{‘topic’:'”+topic+“‘,‘app’:’”+app+“',‘messages’:[{‘businessId’:‘id1’,‘text’:‘abc’,‘attributes’:{‘p1’:‘aaaa’,‘p2’:‘bbb’,‘p3’:‘ccc’}},{‘businessId’:‘id2’,‘text’:‘def’}]}”;
    try {
    if(servers == null || servers.length == 0) {
    testAuth();
    continue;
    }else {
    String str = httpclient(“http://”+servers[0]+“/1.0/produce”, put);
    System.out.println(“put:”+put);
    JSONObject jsonObject = JSON.parseObject(str);
    JSONObject statusObj = jsonObject.getJSONObject(“status”);
    if(statusObj.getIntValue(“code”)==0){
    //发送成功
    }else if(statusObj.getIntValue(“code”)==2){
    //认证失败,重新认证
    servers = null;
    testAuth();
    continue;
    }
    }

         } catch (Exception e) {
             e.printStackTrace();
             break;
         }
     }
    

    }

    @Test
    public void testGet(){
    Random r = new Random();

     for(int i=0;i<10000;i++) {
         String get = "{'topic':'"+topic+"','app':'"+app+"'}";
         try {
             if(servers == null || servers.length == 0) {
                 testAuth();
                 continue;
             }else {
                 String str = httpclient("http://"+servers[0]+"/1.0/consume", get);
                 System.out.println("get:" + get);
                 JSONObject jsonObject = JSON.parseObject(str);
                 JSONObject statusObj = jsonObject.getJSONObject("status");
                 //处理业务逻辑
                 if(statusObj.getIntValue("code")==0){
                     //发送成功
                 }else if(statusObj.getIntValue("code")==2){
                     //认证失败,重新认证
                     servers = null;
                     testAuth();
                     continue;
                 }
                 if(r.nextInt(100)<3){
                     //业务报错转重试服务,继续消费
                     String rz = httpclient("http://"+servers[0]+"/1.0/retry", retry(str));
                     JSONObject result = JSON.parseObject(rz);
                     statusObj = result.getJSONObject("status");
                 }else{
                     //消费确认
                     String rz = httpclient("http://"+servers[0]+"/1.0/ack", ack(str));
                     JSONObject result = JSON.parseObject(rz);
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
             break;
         }
     }
    

    }

    public String ack(String str){
    JSONObject jsonObject = JSON.parseObject(str).getJSONObject(“result”);
    JSONArray messages = jsonObject.getJSONArray(“messages”);
    HttpAckMessage ackMessage = new HttpAckMessage();
    ackMessage.setAddress(jsonObject.getString(“address”));
    ackMessage.setApp(jsonObject.getString(“app”));
    ackMessage.setConsumerId(jsonObject.getString(“consumerId”));
    ackMessage.setTopic(jsonObject.getString(“topic”));
    ackMessage.setBrokerGroup(jsonObject.getString(“brokerGroup”));

     List<HttpAckLocation> locations = new ArrayList<HttpAckLocation>();
     if(messages == null){
         return null;
     }else{
         System.out.println("messages:"+messages.size());
     }
     for(int i=0;i<messages.size();i++){
         JSONObject msg = messages.getJSONObject(i);
         String topic = msg.getString("topic");
         short queueId = msg.getShortValue("queueId");
         long queueOffset = msg.getLong("queueOffset");
         long journalOffset = msg.getLong("journalOffset");
    
         HttpAckLocation loc = new HttpAckLocation();
         loc.setTopic(topic);
         loc.setQueueId(queueId);
         loc.setQueueOffset(queueOffset);
         loc.setJournalOffset(journalOffset);
         locations.add(loc);
     }
     ackMessage.setLocations(locations.toArray(new HttpAckLocation[locations.size()]));
    
     String ack = JSON.toJSONString(ackMessage);
     System.out.println(ack);
     return ack;
    

    }

    public String retry(String str){
    JSONObject jsonObject = JSON.parseObject(str).getJSONObject(“result”);
    JSONArray messages = jsonObject.getJSONArray(“messages”);
    HttpRetryMessage ackMessage = new HttpRetryMessage();
    ackMessage.setAddress(jsonObject.getString(“address”));
    ackMessage.setApp(jsonObject.getString(“app”));
    ackMessage.setConsumerId(jsonObject.getString(“consumerId”));
    ackMessage.setTopic(jsonObject.getString(“topic”));
    ackMessage.setBrokerGroup(jsonObject.getString(“brokerGroup”));

     List<HttpAckLocation> locations = new ArrayList<HttpAckLocation>();
     if(messages == null){
         return null;
     }else{
         System.out.println("messages:"+messages.size());
     }
     for(int i=0;i<messages.size();i++){
         JSONObject msg = messages.getJSONObject(i);
         String topic = msg.getString("topic");
         short queueId = msg.getShortValue("queueId");
         long queueOffset = msg.getLong("queueOffset");
         long journalOffset = msg.getLong("journalOffset");
    
         HttpAckLocation loc = new HttpAckLocation();
         loc.setTopic(topic);
         loc.setQueueId(queueId);
         loc.setQueueOffset(queueOffset);
         loc.setJournalOffset(journalOffset);
         locations.add(loc);
     }
     ackMessage.setLocations(locations.toArray(new HttpAckLocation[locations.size()]));
     ackMessage.setExecption("test random exception");
     String ack = JSON.toJSONString(ackMessage);
     System.out.println(ack);
     return ack;
    

    }

    public String httpclient(String host,String content) throws Exception {
    HttpClientBuilder builder = HttpClientBuilder.create();
    RequestConfig.Builder confBuild = RequestConfig.custom();
    confBuild.setConnectTimeout(5000);
    confBuild.setConnectionRequestTimeout(30*1000);

     builder.setDefaultRequestConfig(confBuild.build());
     CloseableHttpClient httpclient = builder.build();
    
     String rs = null;
     ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
    
         public String handleResponse(
                 final HttpResponse response) throws ClientProtocolException, IOException {
             int status = response.getStatusLine().getStatusCode();
             if (status >= 200 && status < 300) {
                 HttpEntity entity = response.getEntity();
                 return entity != null ? EntityUtils.toString(entity,Charset.forName("UTF-8")) : null;
             } else {
                 throw new ClientProtocolException("Unexpected response status: " + status);
             }
         }
    
     };
    
     try {
         HttpPost httppost = new HttpPost(host);
         httppost.addHeader("User-Agent","JMQ-PHP/1.0.1");
         httppost.addHeader("Accept","text/plain");
         httppost.addHeader("Host","api.jmq.jd.com");
         httppost.addHeader("Timestamp",String.valueOf(System.currentTimeMillis()));
         httppost.addHeader("authid",authid);
         httppost.setEntity(new StringEntity(content, Charset.forName("UTF-8")));
         System.out.println("Executing request: " + httppost.getRequestLine());
    
         rs = httpclient.execute(httppost, responseHandler);
         System.out.println("Executing response: " + rs);
    
     } finally {
         httpclient.close();
     }
     return rs;
    

    }

    public class HttpAckLocation {

     // 队列偏移
     private long queueOffset;
     // 日志偏移量
     private long journalOffset;
     // 主题
     protected String topic;
     // 队列ID(>=1)
     protected short queueId;
    
     public long getQueueOffset() {
         return queueOffset;
     }
    
     public void setQueueOffset(long queueOffset) {
         this.queueOffset = queueOffset;
     }
    
     public long getJournalOffset() {
         return journalOffset;
     }
    
     public void setJournalOffset(long journalOffset) {
         this.journalOffset = journalOffset;
     }
    
     public String getTopic() {
         return topic;
     }
    
     public void setTopic(String topic) {
         this.topic = topic;
     }
    
     public short getQueueId() {
         return queueId;
     }
    
     public void setQueueId(short queueId) {
         this.queueId = queueId;
     }
    

    }

    public class HttpAckMessage {
    private String topic;
    private String app;
    private String brokerGroup;
    private String address;
    private String consumerId;
    private HttpAckLocation[] locations;

     public HttpAckLocation[] getLocations() {
         return locations;
     }
    
     public void setLocations(HttpAckLocation[] locations) {
         this.locations = locations;
     }
    
     public String getConsumerId() {
         return consumerId;
     }
    
     public void setConsumerId(String consumerId) {
         this.consumerId = consumerId;
     }
    
     public HttpAckMessage() {
    
     }
    
     public String getTopic() {
         return topic;
     }
    
     public void setTopic(String topic) {
         this.topic = topic;
     }
    
     public String getApp() {
         return app;
     }
    
     public void setApp(String app) {
         this.app = app;
     }
    
     public String getBrokerGroup() {
         return brokerGroup;
     }
    
     public void setBrokerGroup(String brokerGroup) {
         this.brokerGroup = brokerGroup;
     }
    
     public String getAddress() {
         return address;
     }
    
     public void setAddress(String address) {
         this.address = address;
     }
    

    }

    public class HttpRetryMessage {
    private String topic;
    private String app;
    private String brokerGroup;
    private String address;
    private String consumerId;
    private String execption;

     private HttpAckLocation[] locations;
    
     public HttpAckLocation[] getLocations() {
         return locations;
     }
    
     public void setLocations(HttpAckLocation[] locations) {
         this.locations = locations;
     }
    
     public String getConsumerId() {
         return consumerId;
     }
    
     public void setConsumerId(String consumerId) {
         this.consumerId = consumerId;
     }
    
     public HttpRetryMessage() {
     }
    
     public String getTopic() {
         return topic;
     }
    
     public void setTopic(String topic) {
         this.topic = topic;
     }
    
     public String getApp() {
         return app;
     }
    
     public void setApp(String app) {
         this.app = app;
     }
    
     public String getBrokerGroup() {
         return brokerGroup;
     }
    
     public void setBrokerGroup(String brokerGroup) {
         this.brokerGroup = brokerGroup;
     }
    
     public String getAddress() {
         return address;
     }
    
     public void setAddress(String address) {
         this.address = address;
     }
    
    
     public String getExecption() {
         return execption;
     }
    
     public void setExecption(String execption) {
         this.execption = execption;
     }
    

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值