Deserialize List with java (Spring Boot).

0

Good afternoon, I'm skirting the typical problem of jackson recursion in a Many-to-Many relationship in Spring Boot, with Custom Deserialize and Custom Serializer. My problem is in Custom Deserialize I need to deserialize this:

{  
        ...
        "authors": [
        {
             "id": 1,
             "name": "test"
        }
    ]
}

I'm trying with this code:

public class CustomDeserializeAuthor extends StdDeserializer<List<Author>> {

public CustomDeserializeAuthor(Class<List<Author>> t) {
    super(t);
}

@Override
public List<Author> deserialize(JsonParser jp, DeserializationContext dc)
        throws IOException, JsonProcessingException {

    long id = 0;
    String name = null;
    String[] languages = null;
    JsonToken currentToken = null;
    List<Author> authors = new ArrayList();

    while ((currentToken = jp.nextValue()) != null) {
        Author author = new Author();
        switch (currentToken) {
            case START_ARRAY:
                switch (currentToken) {
                    case VALUE_NUMBER_INT:
                        if (jp.getCurrentName().equals("id")) {
                            author.id = jp.getLongValue();
                        }
                        break;
                    case VALUE_STRING:
                        switch (jp.getCurrentName()) {
                            case "name":
                                author.nome = jp.getText();
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
                }
            default:
                break;
        }
        authors.add(author);
    }
    return authors;
}

And I'm getting this error:

"error": "Unsupported Media Type",
"message": "Content type 'application/json;charset=UTF-8' not supported",

I do not have much experience with jackson, so I would like to ask for suggestions on how to solve this problem, Thanks!

    
asked by anonymous 21.12.2018 / 14:37

0 answers