HelloJsonResponse.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package hellowicket;
  2. import java.io.IOException;
  3. import org.apache.wicket.request.http.WebResponse;
  4. import org.apache.wicket.request.resource.IResource;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
  7. public class HelloJsonResponse implements IResource
  8. {
  9. private static final long serialVersionUID = 1L;
  10. private static final String HELLO_WORLD = "Hello, World!";
  11. public static final String APPLICATION_JSON = "application/json";
  12. public static final ObjectMapper MAPPER = new ObjectMapper();
  13. static {
  14. MAPPER.registerModule(new AfterburnerModule());
  15. }
  16. @Override
  17. public void respond(Attributes attributes)
  18. {
  19. try
  20. {
  21. final WebResponse webResponse = (WebResponse) attributes.getResponse();
  22. webResponse.setContentLength(27);
  23. webResponse.setContentType(APPLICATION_JSON);
  24. JsonMessage message = new JsonMessage(HELLO_WORLD);
  25. byte[] json = MAPPER.writeValueAsBytes(message);
  26. webResponse.write(json);
  27. }
  28. catch (IOException ex)
  29. {
  30. // do nothing
  31. }
  32. }
  33. }