将查询参数映射到@ModelAttribute 不尊重@JsonProperty 名称

Mapping query parameters to @ModelAttribute does not respect @JsonProperty name

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

Item 具有属性

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

我尝试了很多方法来获取从 item_type 映射的 itemType 属性。

我该如何解决这个问题?

顺便说一句。我只有传入而不是传出 JSON 序列化 Item.

有这个问题

2017 年 4 月 24 日更新:

下面是一个演示问题的最小示例: 访问 /item 时,您会看到 'outgoing' JSON 序列化有效,但访问 /item/search?name=foo&item_type=bar 时,它不适用于 'incoming' JSON 反序列化。

项目

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

ItemController.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");
    }   
}

JacksonConfiguration.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);
    }
} 

SampleBootApplication.java

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

application.properties

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

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sample</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>nz.net.ultraq.thymeleaf</groupId>
                    <artifactId>thymeleaf-layout-dialect</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- Spring Boot Actuator displays build-related information if a META-INF/build-info.properties 
                            file is present -->
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                        <configuration>
                            <additionalProperties>
                                <encoding.source>${project.build.sourceEncoding}</encoding.source>
                                <encoding.reporting>${project.reporting.outputEncoding}</encoding.reporting>
                                <java.source>${maven.compiler.source}</java.source>
                                <java.target>${maven.compiler.target}</java.target>
                            </additionalProperties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

If /item?name=foo&item_type=bar url 不是来自任何形式并且 如果您只想从 url 中获取姓名和 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

}

注意:如果这是来自任何类型的表单,那么最好使用 POST 而不是 GET

通过在没有 Spring 帮助的情况下完成 Jackson 的工作来解决。

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

你也可以使用HttpServletRequest对象来获取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;
    }

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

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 class 作为控制器方法参数,而不必在每个方法中实例化每个对象:

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