我是Spring的新手,试图创建一个MQTT客户端服务。我有简单的自动处理项目,并希望按照https://docs.spring.io/spring-integration/reference/html/mqtt.html中的建议尝试Paho客户端的xml配置。
DemoApplication.java:
@Configuration
@ImportResource( { "mqtt.xml" } )
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
mqtt.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="clientFactory" class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
<property name="userName" value="${mqtt.username}"/>
<property name="password" value="${mqtt.password}"/>
</bean>
<int-mqtt:message-driven-channel-adapter id="mqttInbound"
client-id=""
url="${mqtt.url}"
topics="sometopic"
client-factory="clientFactory"
channel="output"/>
</beans>
我有一个自动生成的(gradlew build
).classpath文件。但是,无论我在哪里尝试存储mqtt.xml文件,在测试时输入gradle build
总是失败,声称:
IOException从ServletContext资源[/mqtt.xml]解析XML文档;嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/mqtt.xml]
...
java.io.FileNotFoundException:无法打开ServletContext资源[/mqtt.xml]
的.classpath:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/main" path="src/main/resources">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
答案 0 :(得分:0)
您必须在Java类路径中保存mqtt.xml文件,即src / main / resources以解决上述问题。