如何在@RestController中命名compex @RequestParams的属性?

时间:2016-07-19 12:15:37

标签: java spring spring-mvc spring-rest

是否可以在Spring中重命名GET Web服务中使用的参数?与以下示例中的search.limitResults类似:

localhost:8080/firstname=test&search.limitResults=10

你明白了。这可以实现吗?

@RestController
public class MyServlet {
   @RequestMapping(value = "/", method = RequestMethod.GET)
   private String test(RestParams p) {

   }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RestParams {
    private String firstname;
    private String lastname;

    //is that possible to nest?
    @XmlElement(name = "search")
    private MyComplexSearch search;
    public MyComplexSearch getSearch() {return search;}
    public void setSearch(MyComplexSearch) {this.search = search;}

    @XmlRootElement(name = "search")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class MyComplexSearch {
       private int limitResults;
       //some more
    }
}

请求不适用于上面的代码。相反,人们必须使用myComplexSearch作为对象名称。

localhost:8080/firstname=test&myComplexSearch.limitResults=10

如何重新定义输入属性的名称,而不必重命名java类本身?

1 个答案:

答案 0 :(得分:0)

嵌套类必须静态

public static class MyComplexSearch