无法使用Scala和Spring MVC 3返回JSON

发布于 2024-12-09 01:35:04 字数 923 浏览 0 评论 0原文

我正在将 Java Spring 控制器类转换为 Scala。在 Java 中,返回 JSON 的控制器方法定义如下:

@RequestMapping(value = "/search", method = RequestMethod.GET)
public @ResponseBody String[] searchFoods(@RequestParam("term") String searchTerm, Principal principal) { ... }

这按预期工作。 Scala 中的相同方法如下所示:

@RequestMapping(value = Array("/search"), method = Array(RequestMethod.GET))
def searchFoods(@RequestParam("term") searchTerm: String, principal: Principal): java.util.List[String] @ResponseBody = { ... }

但是,每当请求此路径时,我都会收到以下异常:

2011-10-09 09:06:19.980:WARN::/searchpath/search.html
javax.servlet.ServletException: Could not resolve view with name 'searchpath/search' in servlet with name 'dispatcher'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)

并且 Web 服务器返回 HTTP 500 错误。是否可以一起使用 Scala 和 Spring MVC 3 返回 JSON?

I am converting my Java Spring controller classes to Scala. In Java, a controller method that returned JSON was defined as this:

@RequestMapping(value = "/search", method = RequestMethod.GET)
public @ResponseBody String[] searchFoods(@RequestParam("term") String searchTerm, Principal principal) { ... }

This works as expected. The same method in Scala looks like this:

@RequestMapping(value = Array("/search"), method = Array(RequestMethod.GET))
def searchFoods(@RequestParam("term") searchTerm: String, principal: Principal): java.util.List[String] @ResponseBody = { ... }

However, any time this path is requested I get the following exception:

2011-10-09 09:06:19.980:WARN::/searchpath/search.html
javax.servlet.ServletException: Could not resolve view with name 'searchpath/search' in servlet with name 'dispatcher'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)

And the web server returns an HTTP 500 error. Is it possible to use Scala and Spring MVC 3 together to return JSON?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

眼眸里的那抹悲凉 2024-12-16 01:35:04

看起来我把 @ResponseBody 放在了错误的地方。它需要出现在方法定义之前:

@RequestMapping(value = Array("/search"), method = Array(RequestMethod.GET))
@ResponseBody
def searchFoods(@RequestParam("term") searchTerm: String, principal: Principal): java.util.List[String]  = { ... }

我以前尝试过这个,但忘记清理我的项目。将注释移动到正确的位置,然后清理和重建后,一切又恢复正常了。谢谢!

Looks like I was putting the @ResponseBody in the wrong place. It needs to appear before the method definition:

@RequestMapping(value = Array("/search"), method = Array(RequestMethod.GET))
@ResponseBody
def searchFoods(@RequestParam("term") searchTerm: String, principal: Principal): java.util.List[String]  = { ... }

I had tried this before, but forgot to clean my project. After moving the annotation to the correct location, then cleaning and rebuilding, everything was working again. Thanks!

-黛色若梦 2024-12-16 01:35:04

看起来问题不是 JSON,而是路径解析。尝试将方法注释更改为:

@RequestMapping(value = Array("/searchpath/search"), method = Array(RequestMethod.GET))

请参阅此页面< /a> 获取完整示例。

Looks like the issue isn't JSON, but path resolution. Try changing the method annotation to:

@RequestMapping(value = Array("/searchpath/search"), method = Array(RequestMethod.GET))

See this page for a full example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文