简体   繁体   English

Spring-从JSON响应到webPage

[英]Spring - from JSON response to webPage

I have built a simple service in Spring: 我在Spring中建立了一个简单的服务:

@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody List<Person> eventList() throws ParseException {
    return personDAOImpl.list();
}

When I invoke the corresponding URL, I see a JsonObject list like this: 当我调用相应的URL时,会看到一个JsonObject列表,如下所示:

[{"id":2,"name":"John"},{"id":3,"name":"Jack"},{"id":4,"name":"Sam"}]

This service will be invoked both from a mobile app (and I know how to use a Json response in this case) and from a browser, and so I want that the /list service returns a Json. 将从移动应用程序(在这种情况下,我知道如何使用Json响应)和浏览器中调用该服务,因此我希望/ list服务返回Json。

By now, if I write the url in a browser (typing localhost:8080/myproject/eventList) I simply obtain the aforementioned Json String displayed on the screen. 到目前为止,如果我在浏览器中输入url(键入localhost:8080 / myproject / eventList),则只需获得上述在屏幕上显示的Json String。

How could I obtain a user-friendly interface that shows to the user a more friendly, like if I used a jsp? 如何获得一个用户友好的界面,向用户显示更友好的界面,就像使用jsp一样?

ps: I have already used a service that successfully returns a jsp page (using ModelAndView), and that shows a table with the items, but I repeat that I would like to get a json response and than parsing it in a different way depending from the client (web browser or mobile app). ps:我已经使用过一项服务,该服务成功返回了一个jsp页面(使用ModelAndView),并显示了一个包含项目的表,但是我重复一遍,我想得到一个json响应,而不是按照以下方式以不同的方式解析它:客户(网络浏览器或移动应用)。

Thank you in advance! 先感谢您!

You could build a client app which makes requests to your API using JavaScript . 您可以构建一个客户端应用程序,该应用程序使用JavaScript向您的API发出请求。

For example, 例如,

  1. localhost:8080/jsp/home returns a jsp page Home. localhost:8080 / jsp / home返回一个jsp页面Home。
  2. The Home page makes AJAX request (using JavaScript) to localhost:8080/myproject/eventList and recieves the JSON object. 主页通过AJAX请求(使用JavaScript)到localhost:8080 / myproject / eventList并接收JSON对象。 jQuery.get() jQuery.get()
  3. The Home page adds (using JavaScript) some visual elements to html. 主页(使用JavaScript)向html添加了一些可视元素。 jQuery.append() jQuery.append()

That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM