无法使用Scala和Spring MVC 3返回JSON
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来我把 @ResponseBody 放在了错误的地方。它需要出现在方法定义之前:
我以前尝试过这个,但忘记清理我的项目。将注释移动到正确的位置,然后清理和重建后,一切又恢复正常了。谢谢!
Looks like I was putting the @ResponseBody in the wrong place. It needs to appear before the method definition:
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!
看起来问题不是 JSON,而是路径解析。尝试将方法注释更改为:
请参阅此页面< /a> 获取完整示例。
Looks like the issue isn't JSON, but path resolution. Try changing the method annotation to:
See this page for a full example.