设置多个请求参数可选

时间:2014-02-06 05:39:23

标签: spring java-ee spring-mvc

我遇到了大问题我想忽略对象的某些属性但不知道如何从同一个请求对象中跳过多个属性

这是我的代码

TestRequestInfo.java

public class TestRequestInfo extends BaseFilter {

    private String id;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }


} 

BaseFilter.java

public class BaseFilter {

    private PagingInfo paging;

    public PagingInfo getPaging() {
        return paging;
    }
    public void setPaging(PagingInfo paging) {
        this.paging = paging;
    }


}

PagingInfo.java

@XmlRootElement(name = "paging")
    public class PagingInfo {
        private Integer totalNumOfRows;
        private Integer minRowNumber;
        private Integer maxRowNumber;

        public PagingInfo() {
            super();
        }

        public PagingInfo(Integer totalNumOfRows, Integer maxRowNumber, Integer minRowNumber) {
            super();
            this.totalNumOfRows = totalNumOfRows;
            this.maxRowNumber = maxRowNumber;
            this.minRowNumber = minRowNumber;
        }

        public PagingInfo(Integer maxRowNumber, Integer minRowNumber) {
            super();
            this.maxRowNumber = maxRowNumber;
            this.minRowNumber = minRowNumber;
        }
        public Integer getTotalNumOfRows() {
            return totalNumOfRows;
        }
        public void setTotalNumOfRows(Integer totalNumOfRows) {
            this.totalNumOfRows = totalNumOfRows;
        }

        public Integer getMinRowNumber() {
            return minRowNumber;
        }

        public void setMinRowNumber(Integer minRowNumber) {
            this.minRowNumber = minRowNumber;
        }

        public Integer getMaxRowNumber() {
            return maxRowNumber;
        }

        public void setMaxRowNumber(Integer maxRowNumber) {
            this.maxRowNumber = maxRowNumber;
        }
    }

控制器

 @RequestMapping(value = { "/testList"} , method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public @ResponseBody String testList(HttpServletRequest request, TestRequestInfo testRequestInfo)
        {
        ....
        ....
        }

要求我做了

http://example/abc/testList?id=&paging.maxRowNumber=&paging.minRowNumber=

对空白属性请求它使它成为错误请求并给出ERROR 400,如果我填充这些参数,它会给出正确的响应。

@RequestParam(value = "i", required=false)仅适用于单一实体,如何minRowNumbermaxRowNumber可选?

提前致谢

1 个答案:

答案 0 :(得分:1)

这是我的代码和测试。

> http://localhost:8080/FileUpload/testList.htm?id=1&paging.maxRowNumber=1&paging.minRowNumber=1
> TestRequestInfo [id=1, getPaging()=PagingInfo [totalNumOfRows=null,
> minRowNumber=1, maxRowNumber=1]]
> 
> http://localhost:8080/FileUpload/testList.htm?id=1&paging.maxRowNumber=1&paging.minRowNumber=
> TestRequestInfo [id=1, getPaging()=PagingInfo [totalNumOfRows=null,
> minRowNumber=null, maxRowNumber=1]]
> 
> http://localhost:8080/FileUpload/testList.htm?id=1&paging.maxRowNumber=&paging.minRowNumber=
> TestRequestInfo [id=1, getPaging()=PagingInfo [totalNumOfRows=null,
> minRowNumber=null, maxRowNumber=null]]



 @RequestMapping(value = { "/testList"} , method=RequestMethod.GET)
 public @ResponseBody String testList(HttpServletRequest request, TestRequestInfo testRequestInfo)
 {

    System.out.println(testRequestInfo);
    return "ss";
 }

我的春季配置

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

正如您所看到的,代码完全正常运行,似乎问题可能是您正在使用的url-pattern。

检查您是否正在使用

<mvc:annotation-driven/>