修改新增接口,可以发送json或者xml类型的消息
This commit is contained in:
@@ -1,18 +1,27 @@
|
|||||||
package com.gzzn.omms.msgexchangeapi.controller;
|
package com.gzzn.omms.msgexchangeapi.controller;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.gzzn.omms.msgexchangeapi.dto.ResponseDto;
|
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.service.IKafkaService;
|
||||||
|
import com.gzzn.omms.msgexchangeapi.utils.JsonUtil;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/kafka")
|
@RequestMapping("/kafka")
|
||||||
public class KafkaController {
|
public class KafkaController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private IKafkaService kafkaservice;
|
private IKafkaService kafkaservice;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
IExchangeService exchangeService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送消息到kafka指定topic上
|
* 发送消息到kafka指定topic上
|
||||||
@@ -20,9 +29,21 @@ public class KafkaController {
|
|||||||
* @param topic
|
* @param topic
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/send")
|
@PostMapping(value = "/topics/{name}/msgs")
|
||||||
public ResponseDto sendKafka(String msg,String topic) {
|
public ResponseDto sendJsonToKafka(@PathVariable("name") String topicName,@RequestBody String msg) {
|
||||||
kafkaservice.msgSend(topic, msg);
|
try {
|
||||||
return ResponseDto.success();
|
//默认以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;
|
package com.gzzn.omms.msgexchangeapi.service;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@@ -44,4 +46,30 @@ public class ExchangeServiceImplTest {
|
|||||||
|
|
||||||
//assertThat(jsonPath("$.", matcher)).
|
//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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user