|
@@ -2,6 +2,7 @@ package org.glassfish.grizzly.bm;
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonFactory;
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
+import com.fasterxml.jackson.databind.*;
|
|
|
import java.io.IOException;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import org.glassfish.grizzly.http.server.HttpHandler;
|
|
@@ -14,7 +15,10 @@ import org.glassfish.grizzly.http.util.Header;
|
|
|
*/
|
|
|
public class JsonHttpHandler extends HttpHandler {
|
|
|
|
|
|
- private final JsonFactory factory = new JsonFactory();
|
|
|
+ // Response message class.
|
|
|
+ public static class HelloMessage {
|
|
|
+ public final String message = "Hello, World!";
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void service(final Request request, final Response response)
|
|
@@ -22,22 +26,16 @@ public class JsonHttpHandler extends HttpHandler {
|
|
|
response.setContentType("application/json");
|
|
|
response.setHeader(Header.Server, Server.SERVER_VERSION);
|
|
|
|
|
|
- JsonGenerator generator = null;
|
|
|
+ ObjectMapper MAPPER = new ObjectMapper();
|
|
|
|
|
|
- try {
|
|
|
- generator = factory.createGenerator(response.getOutputStream());
|
|
|
- generator.writeStartObject();
|
|
|
- generator.writeStringField("message", "Hello, world");
|
|
|
- generator.writeEndObject();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new IllegalStateException(e);
|
|
|
- } finally {
|
|
|
- if (generator != null) {
|
|
|
- try {
|
|
|
- generator.close();
|
|
|
- } catch (IOException e) {
|
|
|
- }
|
|
|
- }
|
|
|
+ // Write JSON encoded message to the response.
|
|
|
+ try
|
|
|
+ {
|
|
|
+ MAPPER.writeValue(response.getOutputStream(), new HelloMessage());
|
|
|
+ }
|
|
|
+ catch (IOException ioe)
|
|
|
+ {
|
|
|
+ // do nothing
|
|
|
}
|
|
|
}
|
|
|
|