gpt4 book ai didi

java - Spring MVC Rest Controller @RequestBody 解析

转载 作者:行者123 更新时间:2023-11-30 08:04:57 26 4
gpt4 key购买 nike

我有一个 spring rest web 应用程序,其中包含一个如下所示的通用 rest Controller 。 GET 方法工作正常,Jackson 将对象序列化为 JSON。但是,当我尝试调用保存方法时,RequestBody 参数被转换为 LinkedHashMap 而不是 T 泛型类型定义的类型。

@RestController
public abstract class CrudAPI<T extends Object, ID extends Serializable>{

@Transactional
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Void> save(@RequestBody T entity){
service.save(entity);
return new ResponseEntity(HttpStatus.CREATED);
}

}

JSON:

{
"id":null,
"version":null,
"name":"Copel",
"disabled":false,
"type":"P",
"labels":[
{
"id":null,
"version":null,
"name":"unidade consumidora"
}
]
}

我收到以下错误:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'version' of bean class [java.util.LinkedHashMap]: Could not find field for property during fallback access!

Spring 配置:

@Configuration
@Import(Application.class)
@EnableWebMvc
@ComponentScan(basePackages = {"br.com.doc2cloud"})
public class WebConfig extends WebMvcConfigurerAdapter implements WebApplicationInitializer {

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

private MappingJackson2HttpMessageConverter jacksonConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate5Module());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(new ISO8601DateFormat());
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
jacksonConverter.setObjectMapper(mapper);

return jacksonConverter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonConverter());
super.configureMessageConverters(converters);
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("javax.servlet.jsp.jstl.fmt.localizationContext", "messages");
EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD);
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("characterEncoding", characterEncodingFilter);
characterEncoding.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
}
}

我的代码有什么问题?

最佳答案

我不认为你可以实现你想要的。 Java 泛型机制仅在编译时起作用。编译后通用类型被删除并被实际类型替换(在您的情况下为 Object )。您的 Controller 无法理解您尝试解析 JSON 数据的类型。

你得到 LinkedHashMap 的原因是 Jackson 默认将它用于“未知”类型。典型的 JSON 数据实际上是一个键值映射,Jackson 保留属性排序 - 这就是使用链接 HashMap 的原因。

关于java - Spring MVC Rest Controller @RequestBody 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322400/

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