Skip to content
CoAP Java library
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea init java-coap project May 11, 2017
coap-core Remove printing payload to log when mismatch block response Sep 17, 2018
example-client enable session resumption in OpensslProcessTransport Mar 14, 2019
pmd-rules.xml 📑 updated copyright header: year Jul 4, 2018

mbed CoAP

CircleCI License codecov Known Vulnerabilities


This library makes it easy to integrate a Java SE enabled device with coap based services like mbed Cloud. It can also help to emulate an embedded device for prototyping and testing purposes.

The following features are supported by the library:

  • Complete CoAP support
    • The Constrained Application Protocol RFC 7252
    • Observing Resources in the Constrained Application Protocol RFC 7641
    • Block-Wise Transfers in the Constrained Application Protocol RFC 7959
  • CoRE Link Format processing API
    • Constrained RESTful Environments (CoRE) Link Format RFC 6690
  • CoAP server mode
  • CoAP client mode
  • Coap over tcp, tls RFC 8323
    • excluding: websockets, observations with BERT blocks
  • Network transports:
    • UDP (plain text)
    • TCP (plain text)
    • TLS
  • LwM2M TLV and JSON data formats



  • JRE 8
  • JRE 11


  • JDK 8
  • maven 3.x

Using the Library

Add repository to build file:


Add dependency into your pom.xml:


Creating a Server

Initializing, starting and stopping the server

To initialize a server, you must at minimum define the port number. You must set the server parameters before starting a server.

CoapServer server = CoapServer.builder().transport(5683).build();

To stop a server, use the stop() method.


Adding request handlers

You can add handlers before or while the server is running. There can be several URI paths assigned to the same handler. You can also remove a handler at any time.

CoapHandler handler = new ReadOnlyCoapResource("24");
server.addRequestHandler("/temperature", handler);


Creating CoAP resources

To create a CoAP resource, you must implement a CoapHandler. There is one abstract helper class CoapResource that can be extended. At minimum, implement the get() method.

The following example overrides get() and put() and creates a simple CoAP resource:

public class SimpleCoapResource extends CoapResource {
    private String body="Hello World";
    public void get(CoapExchange ex) throws CoapCodeException {
        ex.setResponseBody("Hello World");
    public void put(CoapExchange ex) throws CoapCodeException {
      body = ex.getRequestBodyString();        

Creating a client

To make a CoAP request, use the class CoapClient. It uses fluent API. The following is a simple example on the usage:

CoapClient client = CoapClientBuilder.newBuilder(new InetSocketAddress("localhost",5683)).build();

CoapPacket coapResp = client.resource("/s/temp").sync().get();

coapResp = client.resource("/a/relay").payload("1", MediaTypes.CT_TEXT_PLAIN).sync().put();
//it is important to close connection in order to release socket

Example client

This example client demonstrates how to build coap client.



mvn clean install

Build with all checks enabled

mvn clean install -P ci

Update license header

mvn com.mycila:license-maven-plugin:format


All contributions are Apache 2.0. Only submit contributions where you have authored all of the code. If you do this on work time make sure your employer is OK with this.


Unless specifically indicated otherwise in a file, files are licensed under the Apache 2.0 license, as can be found in: LICENSE

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.