Java Spring 3 JSON 与 MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1601992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3 JSON with MVC
提问by stevedbrown
Is there a way to build Spring Webcalls that consume and produce application/json
formatted requests and responses respectively?
有没有办法构建分别使用和生成格式化请求和响应的Spring Web调用application/json
?
Maybe this isn't Spring MVC, I'm not sure. I'm looking for Spring libraries that behave in a similar fashion to Jersey/JSON. The best case would be if there was an annotation that I could add to the Controller classes that would turn them into JSONservice calls.
也许这不是Spring MVC,我不确定。我正在寻找与Jersey/JSON行为类似的 Spring 库。最好的情况是如果有一个注释,我可以添加到控制器类中,将它们转换为JSON服务调用。
A tutorial showing how to build Spring Web Serviceswith JSON would be great.
展示如何使用 JSON构建Spring Web 服务的教程会很棒。
EDIT:I'm looking for an annotation based approach (similar to Jersey).
编辑:我正在寻找一种基于注释的方法(类似于 Jersey)。
EDIT2:Like Jersey, I am looking for REST support (POST,GET,DELETE,PUT).
EDIT2:和 Jersey 一样,我正在寻找 REST 支持(POST、GET、DELETE、PUT)。
EDIT3:Most preferably, this will be the pom.xml
entries and some information on using the spring-js with Hymanson Spring native version of things.
EDIT3:最理想的是,这将是pom.xml
关于将 spring-js 与 Hymanson Spring 本机版本一起使用的条目和一些信息。
采纳答案by Johan
In case other ppl get here later: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/was the most useful link for me. That finally made me understand the model (after less than a day of searching).
如果其他人稍后到达这里:http: //blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/对我来说是最有用的链接。这最终让我理解了模型(经过不到一天的搜索)。
回答by matt b
You can certainly have controllers that return a View which is in JSON output. Take a look at the Spring-JSON project.
您当然可以拥有返回 JSON 输出中的视图的控制器。查看Spring-JSON 项目。
To consume requests in JSON I would assume you would just want a controller to pass a request parameter off to a JSON library which could parse the data?
要使用 JSON 中的请求,我假设您只希望控制器将请求参数传递给可以解析数据的 JSON 库?
回答by jitter
回答by non sequitor
There is no pre-packaged way to do what you want as Jersey is nicely integrated with Spring via the Jersey-SpringAPI so there's really no reason to re-invent the wheel.
没有预先打包的方式来做你想做的事,因为 Jersey 通过Jersey-SpringAPI与 Spring 很好地集成,所以真的没有理由重新发明轮子。
回答by denis.zhdanov
回答by Miguel Ping
You can configure Spring MVC interceptors through handler mappingsand then use the mappings to transform the returning data to JSON. The handler configuration can be done in xml (no need to recompile), and this can be completely transparent to the rest of the application.
您可以通过处理程序映射配置 Spring MVC 拦截器,然后使用映射将返回的数据转换为 JSON。处理程序配置可以在 xml 中完成(无需重新编译),这对应用程序的其余部分是完全透明的。
It's fairly easy to annotate controllers with some annotation, and then hook up the BeanFactory bootstrap process to register the controllers within some handler mapping process.
用一些注解来注解控制器是相当容易的,然后连接 BeanFactory 引导进程以在一些处理程序映射过程中注册控制器。
I have used this approach to transform the result from spring controllers into GWT RPC calls.
我使用这种方法将结果从 spring 控制器转换为 GWT RPC 调用。
You can also use Spring AOP to intercept controller method calls and unwrap/wrap the requests from/to JSON.
您还可以使用 Spring AOP 拦截控制器方法调用并解包/包装来自/到 JSON 的请求。
回答by Paul Whelan
Since spring-mvc 3.0 official support for Ajax remoting with JSON is provided as part of Spring MVC. This includes support for generating JSON responses and binding JSON requests using the Spring MVC @Controller programming model.
由于 spring-mvc 3.0 对使用 JSON 进行 Ajax 远程处理的官方支持是作为 Spring MVC 的一部分提供的。这包括支持使用 Spring MVC @Controller 编程模型生成 JSON 响应和绑定 JSON 请求。
see here
看这里
回答by Patrick Salami
This feature is now part of Spring since version 3.0. You can simply use the @ResponseBody annotation to specify that you want the return value from your request handler methods to be serialized to JSON and sent as the response body. See http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
自 3.0 版以来,此功能现已成为 Spring 的一部分。您可以简单地使用 @ResponseBody 注释来指定您希望将请求处理程序方法的返回值序列化为 JSON 并作为响应正文发送。见http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/