0

I am trying to pass a JSON request to my server where the controller encounters an error while converting the JSON to POJO.

JSON Request

{ 
   "request":[
      {"name":"mac"},
      {"name":"rosy"}
   ]
}

My controller function

@RequestMapping(value = "/namelist", 
            method = RequestMethod.POST,
            consumes = { "application/json" },
            produces = {"application/json"})
public ... postNameList(@RequestBody NameList names) {}


Public Class NameList extends ArrayList<Name> {}
Public Class Name { private name; ...}

Error

message: "Could not read JSON: Can not deserialize instance of com.abc.xyz.mypackage.NameList out of START_OBJECT token at [Source: org.eclipse.jetty.server.HttpConnection$Input@79aac24b{HttpChannelOverHttp@1d109942{r=1,a=DISPATCHED,uri=/namelist},HttpConnection@2cbdcaf6{FILLING},g=HttpGenerator{s=START},p=HttpParser{s=END,137 of 137}}; line: 1, column: 1]

I am not sure what's wrong with the code. I am fairly new to Spring so any help is appreciated.

rbento
  • 9,919
  • 3
  • 61
  • 61
M10TheMist
  • 397
  • 1
  • 4
  • 15

2 Answers2

0

Your POJO classes should look like this:

class Request {
    private List<Name> request;

    // getters, setters, toString, ...
}

class Name {
    private String name;

    // getters, setters, toString, ...
}

Usage:

@RequestMapping(value = "/namelist", 
            method = RequestMethod.POST,
            consumes = { "application/json" },
            produces = {"application/json"})
public ... postNameList(@RequestBody Request request) { ... }
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
0

I faced similar situation and then created utility to convert JSON objects into Java Objects. Hope this helps.

Here sample.json is the file you want to a Java Object

import com.sun.codemodel.JCodeModel;
import org.jsonschema2pojo.*;
import org.jsonschema2pojo.rules.RuleFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

/**
 * Created by Pratik Ambani
 */
class JsonToPojo {

    public static void main(String[] args) {
        String packageName = "com.practise";
        File inputJson = null;
        try {
            inputJson = ResourceUtils.getFile("classpath:sample.json");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        File outputPojoDirectory = new File("." + File.separator + "Generated Pojo");
        outputPojoDirectory.mkdirs();

        try {
            new JsonToPojo().convert2JSON(inputJson.toURI().toURL(), outputPojoDirectory, packageName, inputJson.getName().replace(".json", ""));
        } catch (IOException e) {
            System.err.println("Encountered issue while converting to pojo: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void convert2JSON(URL inputJson, File outputPojoDirectory, String packageName, String className) throws IOException {
        JCodeModel codeModel = new JCodeModel();
        GenerationConfig config = new DefaultGenerationConfig() {
            @Override
            public boolean isGenerateBuilders() { // set config option by overriding method
                return true;
            }

            @Override
            public SourceType getSourceType() {
                return SourceType.JSON;
            }
        };
        SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
        mapper.generate(codeModel, className, packageName, inputJson);
        codeModel.build(outputPojoDirectory);
    }
}
Pratik Ambani
  • 2,523
  • 1
  • 18
  • 28