Java EE 7 Application Developer — Question 55
You need to develop a chat application, which allows the display of emoticons and images together with text messages.
How should you configure a WebSocket endpoints to receive both text and binary messages?
Answer options
- A. Create two @onMessage methods in the same endpoint with appropriate parameter types.
- B. Define the @onMessage methods in your endpoint with Object as parameter and check the actual type in your code.
- C. You can achieve this only by creating separate WebSocket endpoints for each message type.
- D. Create two @onMessage methods, each with appropriate decoder attribute in the same endpoint.
Correct answer: A
Explanation
The correct answer is A because having two @onMessage methods with different parameter types allows the endpoint to handle both text and binary messages seamlessly. Option B is incorrect since using Object requires additional type checking, which complicates the implementation. Option C is wrong as it is unnecessary to create separate endpoints for different message types. Option D is not the best choice; while decoders can help, it doesn't align with the need for distinct methods for different message types.