前奏:json格式字符串與Java對象的相互轉換方法
第一種:com.fasterxml.jackson.databind.ObjectMapper 包
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
//json字符串轉成Java對象
Myclass myclass = objectMapper.readValue(jsonStr , Myclass.class);
//Java對象轉成json字符串
String jsonStr = objectMapper.writeValueAsString(myclass);
第二種: com.alibaba.fastjson.JSONObject 包
import com.alibaba.fastjson.JSONObject;
// json轉換成java對象
Myclass myclass = JSONObject.parseObject(jsonStr , Myclass.class);
//java對象轉換成json
String jsonObject = JSONObject.toJSONString(myclass);
第三種:net.sf.json.JSONObject 包
import net.sf.json.JSONObject;
JSONObject jsonobject = JSONObject.fromObject(jsonStr);
//json字符串轉成java對象
Myclass myclass = (MYclass)JSONObject.toBean(jsonobject,Myclass.class);
//java對象轉換成json
JSONObject jsonStu = JSONObject.fromObject(testMQ);
String javaToJson=jsonStu.toString();
第四種:net.sf.json.JSONArray 包
import net.sf.json.JSONArray;
//把JSON字符串轉換為JAVA對象數組
JSONArray json = JSONArray.fromObject(userStr);//userStr是json字符串
List<Myclass> myclasses = (List<Myclass>)JSONArray.toCollection(json,Myclass.class);
第一種:生產者發送json格式數據(將對象轉成json然后發送,或者直接發送json數據),消費者接收json格式然后轉換成對象進行消費。
第二種:生產者直接發送對象數據,消費者接收對象並進行消費。
需要注意的是,當生產者發送對象的話,需要將對象序列化,消費者將對象進行反序列化即可。
要發送的對象類
public class TestMQ implements Serializable {
private String name;
private Integer age;
//setter/getter以及toString()方法
}
發送消息的接口
/**
* 發送消息的接口,如果要發送消息就實現這個接口
* @author Administrator
*/
public interface MQProducer {
/**
* 發送消息到指定隊列
* @param queueKey
* @param
*/
public void sendDataToQueue(String exchange, String queueKey, Object object);
}
/**
* 實現了發送消息的接口,實現里面的方法進行發送消息
* @author Administrator
*/
@SuppressWarnings("ALL")
@Component
public class RabbitMQProducer implements MQProducer {
@Resource(name="amqpTemplate")
private AmqpTemplate amqpTemplate;
@Override
public void sendDataToQueue(String exchange, String queueKey, Object object) {
System.out.println("sendDataToQueue --"+amqpTemplate);
try {
amqpTemplate.convertAndSend(exchange, queueKey, object);
System.out.println("------------消息發送成功");
} catch (Exception e) {
System.out.println(e);
}
}
}
@Resource
private MQProducer rabbitMQProducer;
/**
* 這里使用rabbitmq是通過配置文件的形式進行使用的
* 主要的內容是:在rabbitmq.xml中配置隊列的相關信息例如connection、exchange等以及隊列消息的監聽器等相關配置
* 定義一個發送消息的接口,使用時實現這個接口,在實現類里重寫AmqpTemplate amqpTemplate 的sendDataToQueue方法。
*
* @throws Exception
*/
@Test
public void MQProducer() throws Exception{
String exchangeName="clsExchange";
String bindingKey="merchant.queue.jasonqueue";
//需要發送的對象
TestMQ testMQ=new TestMQ();
testMQ.setAge(8101);
testMQ.setName("javatojson");
//將對象通過字節流和對象輸出流寫出去
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(testMQ);
byte[] javaByte=bo.toByteArray();
//生產者直接發送對象
rabbitMQProducer.sendDataToQueue(exchangeName,bindingKey,javaByte);
}
通過監聽消費這個消息
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class TestClsConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
//字節碼轉化為對象
byte[] bytes=message.getBody();
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = null;
try {
oi = new ObjectInputStream(bi);
TestMQ testMQ=(TestMQ) oi.readObject();
System.out.println("消費者直接接收對象進行消費(進行處理一些業務):"+testMQ.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
參考博文:
(1) https://www.cnblogs.com/fpqi/p/9722235.html (json格式轉換成對象相關方法)
(2) https://blog.csdn.net/ws379374000/article/details/77982163 (json數組和List集合轉換)
(3) https://blog.csdn.net/east123321/article/details/78900791?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.compare&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.compare (rabbitmq發送對象)