|
@@ -1,31 +1,36 @@
|
|
|
package hellowicket;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.wicket.request.resource.AbstractResource;
|
|
|
-import com.fasterxml.jackson.databind.*;
|
|
|
+import org.apache.wicket.util.collections.MiniMap;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
public class HelloJsonResponse extends AbstractResource
|
|
|
{
|
|
|
- private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
- private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
+ private static final String CONTENT_TYPE = "application/json";
|
|
|
+ private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
- protected ResourceResponse newResourceResponse(Attributes attributes)
|
|
|
- {
|
|
|
- ResourceResponse response = new ResourceResponse();
|
|
|
- response.setContentType("application/json");
|
|
|
- response.setWriteCallback(new WriteCallback() {
|
|
|
+ protected ResourceResponse newResourceResponse(Attributes attributes)
|
|
|
+ {
|
|
|
+ final ResourceResponse response = new ResourceResponse();
|
|
|
+ response.setContentLength(27);
|
|
|
+ response.setContentType(CONTENT_TYPE);
|
|
|
+ response.setWriteCallback(new WriteCallback()
|
|
|
+ {
|
|
|
public void writeData(Attributes attributes)
|
|
|
{
|
|
|
- Map<String, String> data = new HashMap<String, String>();
|
|
|
+ Map<String, String> data = new MiniMap<>(1);
|
|
|
data.put("message", "Hello, World!");
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- attributes.getResponse().write(HelloJsonResponse.mapper.writeValueAsString(data));
|
|
|
+ String json = HelloJsonResponse.mapper.writeValueAsString(data);
|
|
|
+ attributes.getResponse().write(json);
|
|
|
}
|
|
|
catch (IOException ex)
|
|
|
{
|
|
@@ -34,5 +39,5 @@ public class HelloJsonResponse extends AbstractResource
|
|
|
}
|
|
|
});
|
|
|
return response;
|
|
|
- }
|
|
|
+ }
|
|
|
}
|