我们是否必须使用与控制器中的pojo对象完全相同的字段发布json对象?

我是新手rest,我有问题将JSON对象从jquery映射到控制器。 我的jquery JSON对象有一些字段缺席,它们存在于控制器上的java对象中。 我是否必须创建新类来映射此类对象,或者有没有办法在不创建新类的情况下映射这些对象?

这是代码

控制器:

@RequestMapping(value = "/createTest", method = RequestMethod.POST,consumes="application/json") @ResponseBody public String createTest(@RequestBody TestJsonDTO testJson) throws JsonProcessingException, IOException { //.... 

TestJsonDTO:

  public class TestJsonDTO { private TestSet testSet; private List questionsInTest; //gettters and setters 

测试集:

 public class TestSet implements Serializable { public TestSet() { } @Id @GeneratedValue private int id; private String name; private int fullmark; private int passmark; String duration; Date createDate = new Date(); Date testDate; boolean isNegativeMarking; boolean negativeMarkingValue; 

MainQuestion:

 public class MainQuestion implements Serializable { private static final long serialVersionUID = 1L; public MainQuestion() { } @Id @GeneratedValue private int id; private String name; 

和我的jquery post方法

 function createTest() { $.ajax({ type : 'POST', url : "http://localhost:8085/annotationBased/admin/createTest", dataType : "json", contentType : "application/json", data : testToJSON(), success : function() { alert("success") }, error : function(msg) { alert("error while saving test"); } }); } function testToJSON() { listOfQuestionForTest = questionToAdd;//array of ids of questions return JSON.stringify({ "testSet.name" : $('#testname').val(), "testSet.fullmark" : parseInt($('#fullmark').val()), "testSet.passmark" : parseInt($('#passmark').val()), "questionsInTest" : listOfQuestionForTest // "testDate":$('#testDate').value() }) } 

JSON.stringify我没有发送TestJsonDto所有字段。 我该如何映射?

你应该这样配置Spring:

 @Configuration public class ServiceContext extends WebMvcConfigurationSupport { @Override public void configureMessageConverters(List> converters) { MappingJackson2HttpMessageConverter converter = this.getMappingJackson2HttpMessageConverter(); converters.add(converter); } @Bean public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = this.getObjectMapper(); mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); return mappingJackson2HttpMessageConverter; } @Bean public ObjectMapper getObjectMapper() { JsonFactory jsonFactory = new JsonFactory(); ObjectMapper objectMapper = new ObjectMapper(jsonFactory); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // this is what you need objectMapper.setSerializationInclusion(Include.NON_NULL); // this is to not serialize unset properties return objectMapper; } } 

这里,Spring配置了一个ObjectMapper ,它不会序列化值为null属性,并且如果缺少某些属性,则在反序列化时不会失败。

编辑:(添加了一些背景和解释)

Spring将HTTP请求主体中的内容转换为POJO(这就是@RequestBody实际上告诉Spring要做的事情)。 此转换由HttpMessageConverter执行,这是一个抽象。 Spring为常见媒体类型提供默认的特定消息转换器,例如String ,JSON,表单字段等。

在您的情况下,您需要告诉Spring如何反序列化传入的JSON,即如何读取您从jQuery发送的JSON以及如何将此JSON转换为您希望在@Controller接收的POJO( TestJsonDTO在你的问题)。

Jackson 2是一个广泛使用的JSON序列化/反序列化库。 它最重要的类是ObjectMapper ,它用于执行实际的序列化和反序列化。 Spring有一个特定的HttpMessageConverter ,它使用Jackson来序列化和反序列化JSON。 这是MappingJackson2HttpMessageConverter ,它可以接收Jackson的ObjectMapper实例,如果要覆盖默认行为,可以配置该实例。

ObjectMapper配置为不序列化POJO中为null属性(即,您的JSON不会将这些属性包含为字段),更重要的是,在反序列化时,如果缺少属性,则将其配置为不会因exception而失败在你的JSON或你的POJO中。 这就是objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 实际上。