繁体   English   中英

Spring-如何在运行时将URL中的请求参数映射到POJO?

[英]Spring - how to map request parameters in the URL to a POJO at run time?

我需要知道Spring Boot在运行时如何将URL中的请求参数映射到POJO。

这是带有参数的示例网址

    http://localhost:8080/api/public/properties?serviceType.in=SALE&title.contains=some text&price.greaterOrEqualThan=500&price.lessOrEqualThan=50000&propertyType.in=HOUSE&locationId.in=1,2&landSize.greaterOrEqualThan=100&landSize.lessOrEqualThan=1000&bedrooms.greaterOrEqualThan=2&bedrooms.lessOrEqualThan=5&bathrooms.greaterOrEqualThan=1&bathrooms.lessOrEqualThan=3&ageType.in=BRAND_NEW

我有一些Criteria类,它们都扩展了PropertyCriteria类。 举个例子,如果请求不包含任何参数,我希望控制器使用PropertyCriteria。 如果请求包含bedrooms参数,则我希望控制器使用HousePropertyCriteria等。 请参见下面的控制器方法示例。

@GetMapping("/public/properties")
public ResponseEntity<List<Property>> 
   getAllPropertiesNested(HttpServletRequest request) {

   if (condition1 == true) {
       EntityOnePropertyCriteria c1 = new EntityOnePropertyCriteria();
       //populate c1 using request object

   } else {
       EntityTwoPropertyCriteria c2 = new EntityTwoPropertyCriteria();
       //populate c2 using request object
   }

}

手动执行此操作的两种方法:1)我想知道您的项目中是否有权访问HttpServletRequest对象。 在这种情况下,可以使用方法request.getParameter(nameParam)填充所需的对象。

2)使用beanutils库并使用方法BeanUtils.copyProperties(dest,source)在控制器中使用“ @RequestParam Map source”并替换要填充的dest对象

我在此链接上找到了答案。

公共静态无效的applyMapOntoInstance(对象实例,地图属性){

    if (properties != null && !properties.isEmpty()) {
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(instance);
        beanWrapper.setAutoGrowNestedPaths(true);

        for (Iterator<?> iterator = properties.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry<String, ?> entry = (Map.Entry<String, ?>) iterator.next();
            String propertyName = entry.getKey();
            if (beanWrapper.isWritableProperty(propertyName)) {
                beanWrapper.setPropertyValue(propertyName, entry.getValue());
            }
        }
    }
}

我的控制器方法现在看起来像:

@GetMapping("/public/properties")
@Timed
public ResponseEntity<List<Property>> getAllPropertiesNested(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {


    HttpHeaders headers = null;
    if (requestContains("bedrooms", request)) {

        HousePropertyCriteria housePropertyCriteria = new HousePropertyCriteria();
        applyMapOntoInstance(housePropertyCriteria, request.getParameterMap());

        Page<HouseProperty> page = housePropertyQueryService.findByCriteriaNested(housePropertyCriteria, pageable);
        headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/public/properties");
        return new ResponseEntity(page.getContent(), headers, HttpStatus.OK);

    } else {
        Page<Property> page = propertyQueryService.findByCriteriaNested(criteria, pageable);
        headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/public/properties");
        return new ResponseEntity(page.getContent(), headers, HttpStatus.OK);
    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM