如何将@PathVariable映射到CustomObject

时间:2016-06-17 06:27:38

标签: spring rest

以下是我的Spring Controller的示例代码

   @Controller
    @EnableWebMvc
    public class ChildController extends ParentController<InterfaceController> implements InterfaceController{

        @Override
        @RequestMapping(value = "/map/list/{name}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
        @ResponseStatus( HttpStatus.OK)
        @ResponseBody   
        public List<Friends> getAllFriendsByName(
            @PathVariable("name") String name,
            @RequestParam(value="pageSize", required=false) String pageSize,
            @RequestParam(value="pageNumber", required=false) String pageNumber,            
            HttpServletRequest request) throws BasicException {

        //Some logic over here;

        return results;
        }

    @Override
        @RequestMapping(value = "/map/{id}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
        @ResponseStatus( HttpStatus.OK)
        @ResponseBody   
        public List<Friends> getAllFriendsById(
            @PathVariable("id") String id,
            @RequestParam(value="pageSize", required=false) String pageSize,
            @RequestParam(value="pageNumber", required=false) String pageNumber,            
            HttpServletRequest request) throws BasicException {

        //Some logic over here;

        return results;
        }

    }

有没有办法可以将路径变量name和id转换为同样的CustomObject,可能看起来像这样:

public class CustomObject {

private String name;
private String id;
//Omitted setters and getters
}

所以我的控制器最终看起来像这样

@Controller
    @EnableWebMvc
    public class ChildController extends ParentController<InterfaceController> implements InterfaceController{

        @Override
        @RequestMapping(value = "/map/list/{name}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
        @ResponseStatus( HttpStatus.OK)
        @ResponseBody   
        public List<Friends> getAllFriendsByName(
            @PathVariable("name") CustomObject name,
            @RequestParam(value="pageSize", required=false) String pageSize,
            @RequestParam(value="pageNumber", required=false) String pageNumber,            
            HttpServletRequest request) throws BasicException {

        //Some logic over here;

        return results;
        }

    @Override
        @RequestMapping(value = "/map/{id}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
        @ResponseStatus( HttpStatus.OK)
        @ResponseBody   
        public List<Friends> getAllFriendsById(
            @PathVariable("id") CustomObject id,
            @RequestParam(value="pageSize", required=false) String pageSize,
            @RequestParam(value="pageNumber", required=false) String pageNumber,            
            HttpServletRequest request) throws BasicException {

        //Some logic over here;

        return results;
        }

    }

我知道它必然不能绑定到这样的物体,但这个想象中的控制器只是为了理解。

基本上我得到的是路径变量中的一个String,我希望使用setter或CustomObject类直接转换为Entity。

希望我能够很好地解释。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:1)

不可能按照你的要求去做。由于您只能访问有界变量而不能访问URL的其他部分。

我误解了这个问题时的回答。可能对其他人有用:

您需要添加使用@InitBinder注释的方法:

@ControllerAdvice
public class CustomEditorsControllerAdvice {
    @InitBinder
    public void initBinderAll(WebDataBinder binder) {
        binder.registerCustomEditor(CustomObject.class, 
                                    new CustomObjectPropertyEditor());

    }
}

CustomObjectPropertyEditor是一个从PropertyEditorSupport延伸的类,用于实现setAsText方法:

public class CustomObjectPropertyEditor extends PropertyEditorSupport {

  @Override
  public void setAsText(String text) throws IllegalArgumentException {
      setValue(convertTextToMyCustomObject(text)));
  }

  private CustomObject convertTextToMyCustomObject(String text) {
      // TODO implement conversion here
  }
}

请注意,我使用了一个用@ControllerAdvice注释的单独类,但如果需要,您也可以直接在initBinderAll类中添加@Controller方法。