|
@@ -11,27 +11,36 @@ import org.springframework.http.converter.json.*;
|
|
|
import org.springframework.http.server.*;
|
|
|
import org.springframework.stereotype.*;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Handles requests for the application home page.
|
|
|
*/
|
|
|
@Controller
|
|
|
-public class HelloJsonController {
|
|
|
-
|
|
|
- @RequestMapping(value = "/json")
|
|
|
- public Object json(HttpServletResponse response)
|
|
|
+public class HelloJsonController
|
|
|
+{
|
|
|
+
|
|
|
+ @RequestMapping(value = "/json", produces = "application/json")
|
|
|
+ @ResponseBody
|
|
|
+ public Message json()
|
|
|
+ {
|
|
|
+ return new Message("Hello, world");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class Message
|
|
|
{
|
|
|
- Map<String, String> json = new HashMap<String, String>();
|
|
|
- json.put("message", "Hello, world");
|
|
|
-
|
|
|
- try {
|
|
|
- new MappingJackson2HttpMessageConverter().write(
|
|
|
- json, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
|
|
|
- } catch (HttpMessageNotWritableException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ private final String message;
|
|
|
+
|
|
|
+ public Message(String message)
|
|
|
+ {
|
|
|
+ this.message = message;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMessage()
|
|
|
+ {
|
|
|
+ return message;
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|