0

I have one rest service for updating an entity. As a web interface I'm using Spring web with Jackson for processing the request bodies.

@PutMapping(value = "/save")
public ResponseEntity<TestDefinitionFullDto> saveForm(@RequestBody TestDefinitionFullDto definitionFullDto) {


    Optional<TestDefinitionFullDto> result = testFormService.save(definitionFullDto);

    return result
            .map(testDefinition -> new ResponseEntity<>(testDefinition, HttpStatus.OK))
            .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

I want to deserialized the request body into this object.

@JsonIgnoreProperties
public class TestDefinitionFullDto {

    private UUID uuid;
    private String name;
    private String description;
    private List<String> groups;

    public TestDefinitionFullDto() {}

    public TestDefinitionFullDto(TestFormDefinitionEntity entity) {
        this.uuid = entity.getUuid();
        this.name = entity.getName();
        this.description = entity.getDescription();
        this.groups = entity.getGroups();
    }

    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<String> getGroups() {
        return groups;
    }

    public void setGroups(List<String> groups) {
        this.groups = groups;
    }
}

The problem is that json in request body contains groups property which is another json, but I would like to leave it when processing as string and deserialize it into list of strings.

{  
   "uuid":"75635347-88b1-4d0e-b72f-c54d6398461b",
   "name":"awfwaf",
   "description":"",
   "groups":[  
      {  
         "_id":0,
         "_name":"",
         "_questions":[  
            {  
               "_id":1,
               "_name":"",
               "_questionFields":[  

               ]
            },
            {  
               "_id":2,
               "_name":"",
               "_questionFields":[  

               ]
            },
            {  
               "_id":3,
               "_name":"",
               "_questionFields":[  

               ]
            },
            {  
               "_id":4,
               "_name":"",
               "_questionFields":[  

               ]
            },
            {  
               "_id":5,
               "_name":"",
               "_questionFields":[  

               ]
            }
         ]
      }
   ]
}

The error output:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@66c1675e; line: 1, column: 91] (through reference chain: cz.rusna.core.forms.views.TestDefinitionFullDto["groups"]->java.util.ArrayList[0])

Is it possible to somehow deserialize groups as String and not as objects?

rusna
  • 366
  • 6
  • 20

0 Answers0