需要来自 RESTful Roo 应用程序的 application/json

发布于 2024-12-07 07:49:07 字数 819 浏览 6 评论 0原文

我使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-12-14 07:49:07

你解决问题了吗,因为我也有同样的问题......?

好的,我确实有一个解决方案:将方法添加到控制器中,并且不要让 AOP 框架添加它们:

@RequestMapping(headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> listJson() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8"); //was app/text
    return new ResponseEntity<String>(Customer.toJsonArray(Customer
            .findAllCustomers()), headers, HttpStatus.OK);
}

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:

@RequestMapping(headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> listJson() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8"); //was app/text
    return new ResponseEntity<String>(Customer.toJsonArray(Customer
            .findAllCustomers()), headers, HttpStatus.OK);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文