(Spring MVC / Jackson)将查询参数映射到@ModelAttribute:LOWERCASE_WITH_UNDERSCORE到SNAKE_CASE失败

时间:2017-04-19 18:58:04

标签: spring spring-mvc spring-boot jackson naming

@GetMapping("item")
public @ResponseBody String get(@ModelAttribute Item item)

Item具有属性

  • name

  • itemType

当我访问/item?name=foo&item_type=bar item时 仅使用name 而不是填充itemType

我尝试了很多东西来从itemType映射item_type属性。

  • Item的{​​{1}}属性中添加了@JsonProperty(" item_type")。 Described here
  • 添加了一个JackonConfiguration,它将propertyNamingStrategy设置为PropertyNamingStrategy.SNAKE_CASE。 Described here
  • 将spring.jackson.property-naming-strategy = SNAKE_CASE添加到我的Spring Boot application.properties文件中。 Described here
  • itemType班级添加了PropertyNamingStrategy。 Described here

我该如何解决这个问题?

顺便说一下。对于Item的传入JSON序列化,我只有这个问题。

更新04/24/17:

在最小样本下面展示问题: 访问Item时,您会看到'传出' JSON序列化可以正常工作,但在访问/item时,它不适用于传入' JSON反序列化。

项目

/item/search?name=foo&item_type=bar

ItemController.java

package sample;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(SnakeCaseStrategy.class)
public class Item implements Serializable {
    private String name;
    @JsonProperty("item_type")
    private String itemType;
    public Item() { }
    public Item(String name, String itemType) {
        this.name = name;
        this.itemType = itemType;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getItemType() {
        return itemType;
    }
    public void setItemType(String itemType) {
        this.itemType = itemType;
    }
}

JacksonConfiguration.java

package sample;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/item")
public class ItemController {
    @GetMapping("search")
    public @ResponseBody Page<Item> search(@ModelAttribute Item probe) {
        System.out.println(probe.getName());
        System.out.println(probe.getItemType());
        //query repo by example item probe here...
        return null;
    }
    @GetMapping
    public Item get() {
        return new Item("name", "itemType");
    }   
}

SampleBootApplication.java

package sample;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;

@Configuration
public class JacksonConfiguration {
    @Bean
    public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
        return new Jackson2ObjectMapperBuilder()
                .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    }
} 

application.properties

package sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleBootApplication.class, args);
    }
}

的pom.xml

logging.level.org.springframework=INFO
spring.profiles.active=dev
spring.jackson.property-naming-strategy=SNAKE_CASE

4 个答案:

答案 0 :(得分:4)

如果您在使用默认参数映射时遇到问题,或者您有一个具有复杂创建逻辑的对象,则可以尝试实现HandlerMethodArgumentResolver。这将允许您将您的类用作控制器方法参数,并在其他地方完成映射。

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;
    }

}

然后您必须在Web应用程序配置中注册:

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

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

}

现在您可以使用Item类作为控制器方法参数,而无需在每个方法中实例化每个对象:

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

答案 1 :(得分:3)

在没有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;
}

答案 2 :(得分:1)

如果/ item?name = foo&amp; item_type = bar url不是来自任何形式的 如果您只想从网址获取name和item_type,那么

试试这个:

@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

}

如果你有很多路径变量,你甚至可以尝试下面的方法,这里所有的路径变量都在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

}

注意:如果这是来自任何形式的FORM,那么最好使用POST而不是GET

答案 3 :(得分:1)

你也可以使用 HttpServletRequest 对象来获取参数

@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;
    }