package com.gzzn.omms.msgexchangeapi.controller; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; 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上 * @param msg * @param topic * @return */ @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(); } }