(Spring MVC / Jackson) Mapping query parameters to @ModelAttribute: LOWERCASE_WITH_UNDERSCORE to SNAKE_CASE fails

天大地大妈咪最大 提交于 2019-12-05 00:17:29

If you are having trouble with the default parameter mapping, or you have an object with complex creation logic, you can try implementing a HandlerMethodArgumentResolver. This will allow you to use your class as a controller method argument and have the mapping done elsewhere.

public class ItemArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().equals(Item.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                              ModelAndViewContainer modelAndViewContainer,
                              NativeWebRequest nativeWebRequest,
                              WebDataBinderFactory webDataBinderFactory) throws Exception {
        Item item = new Item();
        item.setName(nativeWebRequest.getParameter("name"));
        item.setItemType(nativeWebRequest.getParameter("item_type"));
        return item;
    }

}

Then you have to register in in your web application configuration:

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
      argumentResolvers.add(new ItemArgumentResolver());
    }

}

Now you can use the Item class as a controller method argument, without having to instantiate each object in each method:

@RequestMapping("/items")
public @ResponseBody String get(Item item){ ... }

Solved by doing the Jackson work without the help of Spring.

@GetMapping("search")
public @ResponseBody Page<Item> search(@RequestParam Map<String,String> params) {
    ObjectMapper mapper = new ObjectMapper();
    //Not actually necessary
    //mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    Item probe = mapper.convertValue(params, Item.class);

    System.out.println(probe.getName());
    System.out.println(probe.getItemType());
    //query repo by example item probe here...
    return null;
}

If /item?name=foo&item_type=bar url is not coming from any kind of form and If you just want to get name and item_type from your url then,

Try This:

@GetMapping("item/{name}/{item_type}")
public String get(@PathVariable("name") String 
myName,@PathVariable("item_type") String myItemType){

//Do your business with your name and item_type path Variable

}

If you have many path variable even you can try bellow approach as well, here all path variables will be in Map,

@GetMapping("item/{name}/{item_type}")
public String get(@PathVariable Map<String,String> pathVars){

//try something like 
   String name= pathVars.get("name");
    String type= pathVars.get("item_type");

//Do your business with your name and item_type path Variable

}

NOTE: if this is from any kind of FORM then better use POST instead of GET

You can also use HttpServletRequest object to get params

@GetMapping("search")
    public @ResponseBody Page<Item> search(HttpServletRequest request) {
       Item probe = new Item();
       probe.setName(request.getParameter('name'));
       probe.setItemType(request.getParameter('item_type'));

        System.out.println(probe.getName());
        System.out.println(probe.getItemType());
        //query repo by example item probe here...
        return null;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!