簡體   English   中英

如何在 Spring MVC 中使 jsonData 不區分大小寫

[英]How to make jsonData case insensitive and in Spring MVC

春天的新人,

我正在嘗試訪問@RequestBody MYPOJO pojo json 對象,該對象工作正常,但我的 json 數據需要與 pojo 中的變量名稱相同並且區分大小寫。 我從網上找到的最好的在這里,但沒有與我的項目同步,我正在使用 spring mvc。 那么如何使用 pojo 使我的 json 不區分大小寫呢?

我接收json的方式

@RequestMapping(value = "create", method = RequestMethod.POST)
public void createPost(HttpServletRequest req, HttpServletResponse resp, @Valid @RequestBody Post post,
        Errors errors) throws CustomException, IOException {

json數據

function jsonForPost(isEdit, id) {
var post = {};
if (isEdit) {
    post.id = id;
}
post.name = $("#name").val();
return JSON.stringify(post);
}

使用Spring Boot

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.MapperFeature;

@Configuration
class Configs {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer initJackson() {
        Jackson2ObjectMapperBuilderCustomizer c = new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder builder) {
                builder.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
            }
        };

        return c;
    }
}

沒有Spring Boot

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.databind.MapperFeature;

    @Configuration
    @EnableWebMvc
    public class AppConfig extends WebMvcConfigurerAdapter {  
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
            builder.indentOutput(true);
            builder.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
            converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
        }   
    }

我有一個帶有變量名稱的 POJO:

public class Pox {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

和控制器:

@RequestMapping(value = "/create", method = RequestMethod.POST)
public void createPost(HttpServletRequest req, HttpServletResponse resp, @Valid @RequestBody Pox post,
        Errors errors) {
    System.out.println(post.getName());


}

我已經用Postman測試過:

姓名,姓名,姓名,姓名。

他們都工作了。

使用application.yml文件的 springboot =>

spring:
  jackson:
    mapper:
      accept-case-insensitive-properties: true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM