【问题标题】:Mapping query parameters to @ModelAttribute does not respect @JsonProperty name(Spring MVC / Jackson)将查询参数映射到@ModelAttribute:LOWERCASE_WITH_UNDERSCORE 到 SNAKE_CASE 失败
【发布时间】:2017-09-16 04:04:51
【问题描述】:
@GetMapping("item")
public @ResponseBody String get(@ModelAttribute Item item)

Item 有属性

  • name

  • itemType

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

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

  • ItemitemType 属性中添加了@JsonProperty("item_type")。 Described here
  • 添加了将 propertyNamingStrategy 设置为 PropertyNamingStrategy.SNAKE_CASE 的 JackonConfiguration。 Described here
  • 在我的 Spring Boot application.properties 文件中添加了 spring.jackson.property-naming-strategy=SNAKE_CASE。 Described here
  • Item 类级别上添加了PropertyNamingStrategy。 Described here

我该如何解决这个问题?

顺便说一句。我只有Item的传入而不是传出JSON序列化有这个问题。


2017 年 4 月 24 日更新:

下面是一个演示问题的最小示例: 访问/item 时,您将看到“传出” JSON 序列化有效,但访问/item/search?name=foo&item_type=bar 时,它不适用于“传入” 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>

【问题讨论】:

  • 如果您在某处有 @EnableWebMvc 注释,请将其删除。参考:stackoverflow.com/questions/40649177/…
  • 没有。
  • @hansi,你的问题解决了我的问题。我遇到了一个问题,我在类中有一个名为 --> private int aScoreEarned; 的字段;当它由基于 JSON 的 Restful API 输出时变为 ascoreEarned。添加 @JsonProperty("aScoreEarned") 注释解决了我的问题。你是个救命的人!

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您在使用默认参数映射时遇到问题,或者您有一个具有复杂创建逻辑的对象,您可以尝试实现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){ ... }
    

    【讨论】:

      【解决方案3】:

      如果 /item?name=foo&item_type=bar url 不是来自任何形式的,并且 如果你只想从你的 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

      【讨论】:

      • 我考虑了这两个选项,但我确实希望获得@ModelAttribute 映射来完成这项工作。为了简单起见,我有很多(> 30)参数并保持较小。
      • 为了让我们使用@ModelAttribute,创建一个与表单输入标签(我假设你使用表单)名称属性完全相同的文件名的 pojo calss,然后 spring 将为你和那里做隐式 DataBinding将不再需要做额外的事情
      • 正如我的问题中所述:这正是我所做的。当涉及到字段名称的下划线到蛇形大小写转换时,它没有按预期工作。
      • 我分享了一个演示上述问题的示例。
      • 实际上没有必要做这些额外的事情,所以你有两个选择 1)要么将你的 pojo 属性更改为 item_type 要么 2)尝试将你的 itemType 的 getter 更改为 getItem_type(),因为在您的案例 Spring 无法为在您的 POJO 中定义为 itemType 的属性 item_type 找到正确的 getter 方法。并且您的 @JsonProperty 不会强制 spring 在 DI 期间使用 item_type 而不是 itemType。
      【解决方案4】:

      您也可以使用 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;
          }
      

      【讨论】:

      • 这并不能解决问题。有大量参数,我不想硬连线所有参数。 (赏金已自动分配)
      • 我说你可以“也使用”它不是最好的解决方案!对我来说,我的所有工作都使用 POST,我更喜欢 @RequestParams 和 get 但有时我们需要从请求中获取参数...
      猜你喜欢
      • 2015-09-30
      • 2021-10-28
      • 1970-01-01
      • 2015-07-01
      • 2019-07-25
      • 2016-01-13
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多