JSON到POJO转换错误

时间:2014-03-13 03:31:26

标签: java json spring jackson pojo

我正在尝试将JSON请求传递到我的服务器,其中控制器在将JSON转换为POJO时遇到错误。

JSON请求

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

我的控制器功能

@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; ...}

错误

  

消息:&#34;无法读取JSON:无法反序列化实例   com.abc.xyz.mypackage.NameList中的START_OBJECT标记位于[来源:   org.eclipse.jetty.server.HttpConnection$Input@79aac24b {HttpChannelOverHttp @ 1d109942 {R = 1,A =分派,URI = /名称列表},HttpConnection的@ 2cbdcaf6 {FILLING},G = {HttpGenerator S = START},P = HttpParser {S = END,137   of 137}}; line:1,column:1]

我不确定代码有什么问题。我对Spring很新,所以感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您的POJO课程应如下所示:

class Request {
    private List<Name> request;

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

class Name {
    private String name;

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

用法:

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

答案 1 :(得分:0)

我遇到了类似的情况,然后创建了将JSON对象转换为Java对象的实用程序。希望这会有所帮助。

sample.json 是您要添加到Java对象的文件

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);
    }
}