(Spring MVC/Jackson)将查询参数映射到@ModelAttribute:LOWERCASE_WITH_UNDERSCORE到SNAKE_CASE失败 [英] (Spring MVC / Jackson) Mapping query parameters to @ModelAttribute: LOWERCASE_WITH_UNDERSCORE to SNAKE_CASE fails

查看:132
本文介绍了(Spring MVC/Jackson)将查询参数映射到@ModelAttribute:LOWERCASE_WITH_UNDERSCORE到SNAKE_CASE失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Item具有属性

  • name

itemType

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

When I access /item?name=foo&item_type=bar the item gets populated only with name not with itemType.

我尝试了很多事情来从item_type映射到itemType属性.

I tried a bunch of things to get the itemType attribute mapped from item_type.

  • ItemitemType属性内添加了@JsonProperty("item_type"). 在此处描述.
  • 添加了一个JackonConfiguration,用于将propertyNamingStrategy设置为PropertyNamingStrategy.SNAKE_CASE. 在此处描述.
  • 已将spring.jackson.property-naming-strategy = SNAKE_CASE添加到我的Spring Boot application.properties文件中. 在此处描述
  • Item类级别上添加了PropertyNamingStrategy. 在此处描述.
  • Added @JsonProperty("item_type") inside Item's itemType attribute. Described here.
  • Added a JackonConfiguration that sets the propertyNamingStrategy to PropertyNamingStrategy.SNAKE_CASE. Described here.
  • Added spring.jackson.property-naming-strategy=SNAKE_CASE to my Spring Boot application.properties file. Described here
  • Added the PropertyNamingStrategy on the Item class level. Described here.

我该如何解决?

顺便说一句.对于Item的传入而不传出JSON序列化,我只有这个问题.

Btw. I only have this problem for incoming not outgoing JSON serialization of Item.

更新04/24/17:

下面是一个最小的示例来演示该问题: 在访问/item时,您会看到传出" JSON序列化有效,但是在访问/item/search?name=foo&item_type=bar时,它对于传入" JSON反序列化不起作用.

Below a minimal sample to demonstrate the problem: When visiting /item you will see that the 'outgoing' JSON serialization works but when visiting /item/search?name=foo&item_type=bar it does not work for 'incoming' JSON deserialization.

项目

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

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

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

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

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>

推荐答案

在没有Spring的帮助下完成了Jackson的工作.

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

这篇关于(Spring MVC/Jackson)将查询参数映射到@ModelAttribute:LOWERCASE_WITH_UNDERSCORE到SNAKE_CASE失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆