Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Handling Custom Objects With JAX-RS SSE API

DZone's Guide to

Handling Custom Objects With JAX-RS SSE API

In this post we take a look at how you can pass non-string types over the wire using JAX-RS SSE. Come check out a quick explanation and some code.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Information sent using JAX-RS 2.1 SSE support (in Java EE 8) does not only have to be of String type – it supports Java primitives (Integer , Long etc.), JSON-B and JAX-B annotated types as well as custom objects whose encoding process (Java object to on-wire format) is defined using a MessageBodyWriter implementation

Here is a simple example you can try out – no need to setup anything except Docker !

Meanwhile – here is the code snippet:

@GET
@Produces("text/event-stream")

public void fetch(@Context Sse sse, @Context SseEventSink eSink) {
OutboundSseEvent stringEvent = sse.newEventBuilder()
.name("stringEvent")
.data(new Date().toString()).build();
eSink.send(stringEvent);

OutboundSseEvent primitiveTypeEvent = sse.newEventBuilder()
.name("primitiveTypeEvent")
.data(System.currentTimeMillis()).build();

eSink.send(primitiveTypeEvent);

OutboundSseEvent jsonbType = sse.newEventBuilder()
.name("jsonbType")
.data(new Employee("test@test", "test", 42))
.mediaType(MediaType.APPLICATION_JSON_TYPE)
.build();
eSink.send(jsonbType);

OutboundSseEvent jaxbType = sse.newEventBuilder()
.name("jaxbType")
.data(new Customer("testcut@test", "king"))
.mediaType(MediaType.APPLICATION_XML_TYPE)
.build();

eSink.send(jaxbType);

OutboundSseEvent customObjWithMBW = sse.newEventBuilder()
.name("customObjWithMBW")
.data(new Student("stud@test", "stud-007")).build();
eSink.send(customObjWithMBW);

System.out.println("events sent");
eSink.close();
System.out.println("sink closed");
}

To summarize

  • Multiple OutboundSseEvents have been created – each differing in the data/media type (text, JSON, XML etc.).
  • The default SSE media type is TEXT_PLAIN, and hence does not need to be explicitly specified when dealing with String data type.
  • Employee class is a JSON-B annotated class.
  • Customer is a JAX-B annotated class.
  • Student has a custom MesaageBodyWriter implementation.

For more details, just refer to the project

Further Reading

Cheers!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ee ,java ,sse ,objects ,api

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}