【pom.xml】
<dependency><groupId>org.eclipse.californium</groupId><artifactId>californium-core</artifactId><version>2.0.0-M7</version>
</dependency>
<dependency><groupId>org.eclipse.californium</groupId><artifactId>element-connector</artifactId><version>2.0.0-M7</version>
</dependency>
<dependency><groupId>org.eclipse.californium</groupId><artifactId>scandium</artifactId><version>2.0.0-M7</version>
</dependency>
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version>
</dependency>
【logback.xml】
<configuration debug="false" xmlns="http://ch.qos.logback/xml/ns/logback"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ch.qos.logback/xml/ns/logbackhttps://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd"><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level ::: %msg%n</pattern></encoder></appender><root level="INFO"><appender-ref ref="STDOUT" /></root>
</configuration>
【MyDemo1CoapServerTest.java】
package com.chz.myCoap.demo1;@Slf4j
public class MyDemo1CoapServerTest {private static String name = MyDemo1CoapServerTest.class.getSimpleName();public static void main(String[] args) {CoapServer server = new CoapServer();// 默认端口5683server.add(new CoapResource("get_hello") {@Overridepublic void handleGET(CoapExchange exchange) {log.info("{}::handleGET: get_hello", name);exchange.respond(ResponseCode.CONTENT, "get_hello: Hello this is MyDemo1CoapServerTest");}});server.add(new CoapResource("post_hello") {@Overridepublic void handlePOST(CoapExchange exchange) {String requestPayload = new String(exchange.getRequestPayload());log.info("{}::handlePOST: post_hello, content={}", name, requestPayload);exchange.respond(ResponseCode.CONTENT, "post_hello: Hello this is MyDemo1CoapServerTest");}});server.add(new CoapResource("time") { // 创建一个资源为time 请求格式为 主机:端口\time@Overridepublic void handleGET(CoapExchange exchange) {log.info("{}::handleGET: time", name);Date date = new Date();exchange.respond(ResponseCode.CONTENT, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));}});server.start();}
}
【MyDemo1CoapClientTest.java】
package com.chz.myCoap.demo1;@Slf4j
public class MyDemo1CoapClientTest {private static String name = MyDemo1CoapClientTest.class.getSimpleName();public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {URI uri = new URI("coap://localhost:5683/get_hello");CoapClient client = new CoapClient(uri);CoapResponse response = client.get();if (response != null) {log.info("code: {}", response.getCode());log.info("options: {}", response.getOptions());log.info("responseText: {}", response.getResponseText());}Thread.sleep(1000L);URI uri2 = new URI("coap://localhost:5683/post_hello");CoapClient client2 = new CoapClient(uri2);String payload = "I am "+name;CoapResponse response2 = client2.post(payload, 0);if (response2 != null) {log.info("code: {}", response2.getCode());log.info("options: {}", response2.getOptions());log.info("responseText: {}", response2.getResponseText());}Thread.sleep(1000L);URI uri3 = new URI("coap://localhost:5683/time");CoapClient client3 = new CoapClient(uri3);CoapResponse response3 = client3.get();if (response3 != null) {log.info("code: {}", response3.getCode());log.info("options: {}", response3.getOptions());log.info("responseText: {}", response3.getResponseText());}}}
启动【MyDemo1CoapServerTest】,然后启动【MyDemo1CoapClientTest】