如何在 spring-boot 中注册自定义 HttpMessageConvertor? [英] How to register a custom HttpMessageConvertor in spring-boot?
问题描述
我正在使用 spring-boot REST 服务,它需要将传入的 json 字段转换为自定义实体.但我无法让自定义转换器工作.
I am using spring-boot REST service and it need to transform incoming json field to custom entities. But I am not able to get the custom converter working.
我确定我在这里遗漏了一些非常明显的东西,但无法弄清楚是什么!
I am sure I am missing something very obvious here but can't figure out what!
我有以下 JSON,我想将其转换为项目实体
I have the following JSON which I want to convert into Item Entity
{
'item_name':'some item name',
'item_category':'some item catagory',
'status_date_flag':'T',
'item_sale_date':'2015-10-28'
}
物品实体
class Item{
private String itemName;
private String catagory;
private boolean isDateFlagOn;
private LocalDate saleDate;
//...
//public getters/setters
}
我创建了一个自定义消息转换器
I have created a custom message converter
@Component
public class ItemMessageConverter extends AbstractHttpMessageConverter<Item> {
public ProductMessageConverter() {
super(MediaType.ALL);
}
@Override
protected boolean supports(Class<?> clazz) {
return clazz.getSimpleName().equalsIgnoreCase(Item.class.getSimpleName());
}
@Override
protected Product readInternal(Class<? extends Item> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
System.out.println("readInternal GETTING called......................");
return null;
}
@Override
protected void writeInternal(Product product, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
System.out.println("writeInternal GETTING called......................");
}
}
我已经创建了一个用于注册 HttpMessageConverter 的配置类
I have created a configuration class for registering the HttpMessageConverter
@Configuration
public class MyBootConfiguration {
@Bean public HttpMessageConverters customConverters(){
HttpMessageConverter itemMessageConverter = new ItemMessageConverter();
return new HttpMessageConverters(itemMessageConverter);
}
}
推荐答案
您尝试做的事情可以通过自定义 Jackson 转换器来实现.像这样更改您的 Item
类定义:
What you are trying to do is achievable by a custom Jackson converter. Change your Item
class definition like this:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(using = ItemDeserializer.class)
public class Item {
private String itemName;
private String category;
private boolean isDateFlagOn;
//constructor, getters and setters
}
注解表明这个类应该通过ItemDeserializer
反序列化,这里是它的实现:
The annotation indicates this class should be deserialized via ItemDeserializer
, here is the implementation of it:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.Objects;
public class ItemDeserializer extends JsonDeserializer {
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String itemName = node.get("itemName").textValue();
String category = node.get("category").textValue();
String flagStr = node.get("isDateFlagOn").textValue();
return new Item(itemName, category, Objects.equals(flagStr, "T"));
}
}
此代码在具有以下处理程序的 Spring 4.2 中工作正常:
This code works fine in Spring 4.2 with following handler:
@Controller
public class GreetingController {
@RequestMapping("jsonGreeting")
public ResponseEntity jsonGreeting(@RequestBody Item item, BindingResult result) throws Exception {
if (result.hasErrors()){
return ResponseEntity.badRequest().body("parse error: " + result.getAllErrors().toString());
}
return ResponseEntity.ok(item.toString());
}
}
这篇关于如何在 spring-boot 中注册自定义 HttpMessageConvertor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!