需要来自 RESTful Roo 应用程序的 application/json
我使用以下 Roo 脚本 (Roo 1.1.5) 创建了一个基本的 RESTful Roo 应用程序。
project --topLevelPackage com.roorest
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity --class ~.domain.MyClass
field string --fieldName String1
web mvc setup
web mvc all --package ~.web
json all
当我访问要求 application/json 的 RESTful WS 时,WS 会输出一个有效的 json 正文,但是内容类型设置为 application/text (如果查看后台生成的 (aj) 控制器代码,这很有意义)。
不幸的是,我需要让 WS 返回 application/json 的内容类型。我尝试从 json-controllers 中推送必要的方法,但这似乎 1) 很麻烦,2) 并没有真正起作用(我在推送源代码时遇到了很多错误)。
可以强制 WS 在一般情况下返回 application/json 吗?例如,是否可以将 ContentNegotiatingViewResolver 与 roo 生成的 aj 控制器结合起来? (为什么 roo 生成的代码首先显式地将应用程序/文本设置为其内容类型?破解 roo JSON 插件是一个可行的解决方案吗?)
我想我真正要问的是: 您认为让 roo 脚手架应用程序通过 WS 以 application/json 形式返回域对象的最佳方法是什么?
I've created a basic RESTful Roo application using the following Roo-script (Roo 1.1.5).
project --topLevelPackage com.roorest
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity --class ~.domain.MyClass
field string --fieldName String1
web mvc setup
web mvc all --package ~.web
json all
When I access the RESTful WS asking for application/json the WS spits out a valid json body, however the content type is set to application/text (which makes perfect sense if one looks at the generated (aj) controller code ticking in the background).
Unfortunately, I need to have the WS return a content type of application/json. I've tried to push in the necessary methods from the json-controllers, however this seems 1) cumbersome, 2) not really working (I'm getting a LOT of errors with the pushed in source).
Can one force the WS return application/json on a general basis? For instance, is it possible to combine ContentNegotiatingViewResolver with the roo generated aj controllers? (And why does the roo generated code explicitly set application/text as it's content type in the first place? Is hacking the roo JSON addon a viable solution?)
I guess what I'm really asking is this: what do you think is the best way to make a roo scaffolded application return domain objects as application/json through a WS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你解决问题了吗,因为我也有同样的问题......?
好的,我确实有一个解决方案:将方法添加到控制器中,并且不要让 AOP 框架添加它们:
Did you solve the problem, because I just having the same one...?
Okay, I do have one solution: Add the methods to your controller and do not let the AOP Framework add them: