java - Spring MVC 在一个请求中返回多个对象

标签 java spring spring-mvc web request

在我的 Controller 中我有这些 ff 方法

  @RequestMapping("/countryList.html")
  @ModelAttribute("countries")
  public Collection<Country> getCountries() {
    return worldService.getAllCountries();
  }

  @RequestMapping("/countryList.html")
  public String getName() {
    return viewers_name;
  }

我试图做的是在 countryList.html 中,它将返回国家和当前查看它的用户的名称,但是在访问 countryList.html 时它返回给我一个异常

Ambiguous handler methods mapped for HTTP path '/countryList.html': {public java.lang.String levelup.world.web.CountryController.getName(), public java.util.Collection levelup.world.web.CountryController.getCountries()}.

我该如何解决这个问题?

最佳答案

@RequestMapping("/countryList.html") 对于方法应该是唯一的。您如何将此请求映射到两种方法。

根据您的评论:-

  @RequestMapping(value = "/countryList.html")
public Collection<Country> getCountries(ModelMap model) {     
        model.addAttribute("countries", countryObject);
   return viewName;

     }

或者在config中定义jsonView返回ajax调用的json对象

 @RequestMapping(value = "/countryList.html")
public Collection<Country> getCountries(ModelMap model) {     
        model.addAttribute("countries", countryObject);
   return jsonView;

     }

关于java - Spring MVC 在一个请求中返回多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067179/

相关文章:

java - 从可调用语句 OUT 参数中检索意外的 null

java - 用 Swagger 表示 ISO 8601 年月日期文档

java - 设置 jdialog 框中文本的格式

java - Spring Boot,具有多个数据源的 Spring Data JPA

java - 加速毕达哥拉斯三重计算器

spring - 获取异常 : No bean named 'springSecurityFilterChain' is defined

java - 如何通过实现自动连接接口(interface)

java - 在您的配置中定义 'bean' 、 "Consider defining a bean named 'EntityManagerFactory' 时遇到麻烦”

ajax - 如何使用基于 Spring MVC 注释的 Portlet Controller 通过 AJAX 呈现 JSON View /响应?

java - Spring Security OAuth Java 配置