jiazx0107@163.com
2023-11-08 0a8ef890592a9c0389c42daeebb9a3f0974c37e6
src/main/java/com/fzzy/mqtt/MqttPubController.java
@@ -2,34 +2,25 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MqttPubController {
    @Autowired
    private MqttGatewayService gatewayService;
    private MqttProviderConfig providerClient;
    @RequestMapping("/hello")
    public String hello() {
        return "hello!";
    @RequestMapping("/sendMessage")
    public @ResponseBody
    String sendMessage(String topic, String message) {
        try {
            providerClient.publish(topic, message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败";
    }
    @RequestMapping("/sendMqtt")
    public String sendMqtt(String sendData) {
        System.out.println(sendData);
        System.out.println("进入sendMqtt-------" + sendData);
        gatewayService.sendToMqtt("topic01", (String) sendData);
        return "Test is OK";
    }
    @RequestMapping("/sendMqttTopic")
    public String sendMqtt(String sendData, String topic) {
        //System.out.println(sendData+"   "+topic);
        //System.out.println("进入inbound发送:"+sendData);
        gatewayService.sendToMqtt(topic, (String) sendData);
        return "Test is OK";
    }
}