修改新增接口,可以发送json或者xml类型的消息

This commit is contained in:
zhouxiunai
2018-11-30 15:03:25 +08:00
parent eb32e8b1e9
commit 48d37b2bf7
2 changed files with 54 additions and 5 deletions
@@ -1,18 +1,27 @@
package com.gzzn.omms.msgexchangeapi.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gzzn.omms.msgexchangeapi.dto.ResponseDto;
import com.gzzn.omms.msgexchangeapi.service.IExchangeService;
import com.gzzn.omms.msgexchangeapi.service.IKafkaService;
import com.gzzn.omms.msgexchangeapi.utils.JsonUtil;
@RestController
@RequestMapping("/kafka")
public class KafkaController {
@Autowired
private IKafkaService kafkaservice;
@Autowired
IExchangeService exchangeService;
/**
* 发送消息到kafka指定topic上
@@ -20,9 +29,21 @@ public class KafkaController {
* @param topic
* @return
*/
@GetMapping(value = "/send")
public ResponseDto sendKafka(String msg,String topic) {
kafkaservice.msgSend(topic, msg);
return ResponseDto.success();
@PostMapping(value = "/topics/{name}/msgs")
public ResponseDto sendJsonToKafka(@PathVariable("name") String topicName,@RequestBody String msg) {
try {
//默认以json 转换
JsonUtil.getObject(msg, Map.class);
kafkaservice.msgSend(topicName, msg);
} catch (Exception e) {
try {
String jsonStr = exchangeService.xmlToJson(msg);
kafkaservice.msgSend(topicName, jsonStr);
} catch (Exception e2) {
throw new RuntimeException(e2);
}
} //end try
return ResponseDto.success();
}
}
@@ -1,6 +1,7 @@
package com.gzzn.omms.msgexchangeapi.service;
import java.io.IOException;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -10,6 +11,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@RunWith(SpringRunner.class)
@SpringBootTest
@@ -44,4 +46,30 @@ public class ExchangeServiceImplTest {
//assertThat(jsonPath("$.", matcher)).
}
@Test
public void testXmlToMap() throws JsonParseException, JsonMappingException, IOException
{
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<MSG xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"unisysaodbsis.xsd\">\r\n" +
" <META>\r\n" +
" <SNDR>AODB</SNDR>\r\n" +
"<SEQN>282266</SEQN>\r\n" +
"<DTTM>20181121224147</DTTM>\r\n" +
"<TYPE>FLOP</TYPE>\r\n" +
"<STYP>DELY</STYP>\r\n" +
"</META>\r\n" +
"<FLOP>\r\n" +
"<FLID>11485800</FLID>\r\n" +
"<FFID>OZ-OZ324-D-22NOV180035-I</FFID>\r\n" +
" <DELY CODE = \"022\" STRT = \"22NOV180035\" DURA = \"0355\">NO REMARKS</DELY>\r\n" +
"</FLOP>\r\n" +
"</MSG>";
XmlMapper xmlMapper = new XmlMapper();
Map map = xmlMapper.readValue(xmlString,Map.class);
}
}