gpt4 book ai didi

java - 如何在 Spring MVC 中使 jsonData 不区分大小写

转载 作者:行者123 更新时间:2023-11-29 08:27:49 28 4
gpt4 key购买 nike

Spring 新品,

我正在尝试访问 @RequestBody MYPOJO pojo 中的 json 对象,它工作正常,但我的 json 数据需要与 pojo 中的变量名称相同并且区分大小写。我从网上找到的最好的是here ,但不与我的项目同步,我正在使用 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;
}


}

和一个 Controller :

@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 测试过:

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

他们都成功了。

关于java - 如何在 Spring MVC 中使 jsonData 不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51063113/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com