Search code examples
javajsonjakarta-eewebsocketjsr356

How can I declare my Java WebSocket endpoint to consume and produce JSON?


How can I convey the information in a declarative way that I need my JSR 356 supported WebSocket server endpoint to consume and produce JSON payload?

If not possible declaratively, what is programmatic way to achieve the same?


Solution

  • No direct annotation for JSON, but you can use:

    @ServerEndpoint( value = "/websocket", 
      encoders = { JsonEncoder.class },   
      decoders = { JsonDecoder.class })
    

    JsonEncoder, JsonDecoder are classes which implements Encoder.Text, Decoder.Text interfaces. You can find sample here: Java EE HTML5 WebSockets Encoder and Decoder example