修改新增接口,可以发送json或者xml类型的消息
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user