杰克逊反对对象内部领域

时间:2018-03-20 14:34:18

标签: java json networking jackson

我正在开发一个java客户端/服务器架构,客户端使用jackson向服务器发送消息。交换数据由消息类定义:

add_action('gform_after_submission_73', 'post_to_third_party', 10, 2);

function post_to_third_party($entry)
{
    $url      = 'http://mywebsitesite.co.uk/webdata.aspx';
    $id = array('clientid' => '15', 'secret' => '123');
    $encoding = 'WINDOWS-1252';
    $brand    = htmlspecialchars($entry['20'], ENT_XML1, $encoding);
    $product  = htmlspecialchars($entry['22'], ENT_XML1, $encoding);
    $form_id  = htmlspecialchars($entry['21'], ENT_XML1, $encoding); 

    $xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>
    <webform>
        <brand>$brand</brand>
        <product>$product</product>
        <form_id>$form_id</form_id>
    </webform>";

     $ch = curl_init($url);

    if ($ch === false) {
        throw new RuntimeException("Unable to initialise a session"); 
    }

    $result = curl_setopt_array($ch, [
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => ['Content-Type: text/xml'],
        CURLOPT_POSTFIELDS => $xml,
        CURLOPT_URL => $url,
        CURLOPT_POSTFIELDS => $id,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => 1,
    ]);

    if ($result === false) {
        throw new RuntimeException("Unable to set session options");
    }

    $output = curl_exec($ch);

    if ($output === false) {
        throw new RuntimeException("Request failed: " . curl_error($ch));
    }

    curl_close($ch);

    echo $output;   
}

由于 pdu 字段,此类可以包含任何对象。例如,可以实例化 Data 对象并将其添加为消息。

public class Message {
    private Header header; //Object that contains only String
    private Object pdu;

    public Message() {
    }

    // Get & Set
    [...]
}

在服务器端,当收到消息时,我想检索嵌套对象(Data)。但是,发生以下异常&#34; com.fasterxml.jackson.databind.node.ObjectNode无法强制转换为Model.Data&#34;当我尝试将pdu转换为Data对象时。如何为任何对象执行该操作。

以下是服务器代码段:

public class Data{
    private String name;
    private String type;

    public Data() {
    }

    // Get & Set
    [...]
}

这里是客户端代码:

Socket socket = serverSocket.accept();
is = new DataInputStream(socket.getInputStream());
os = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(is));

ObjectMapper mapper = new ObjectMapper();
Message message = mapper.readValue(in.readLine(), Message.class);

Data pdu = (Data) message.getPdu(); // Exception here

注意:客户端发送并由服务器接收的邮件格式如下:Message msg = new Message(header, new Data("NAME", "TYPE")); ObjectMapper mapper = new ObjectMapper(); String jsonStr = mapper.writeValueAsString(msg); PrintWriter pw = new PrintWriter(os); pw.println(jsonStr); pw.flush();

1 个答案:

答案 0 :(得分:1)

没有办法只从<transition name="condition ? 'fade' : ''"> <p>Hello</p> </transition> JSON中找出pdu Java类型。如果{"name":"NAME", "type":"TYPE"}可以存储多个不同的对象类型(当前它被声明为pdu),则必须使用JSON字段来告诉Jackson什么是实际的Java类型,例如使用Object

@JsonTypeInfo

另一种方法是为@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type") @JsonSubTypes({ @JsonSubTypes.Type(value = DataA.class, name = "data-a"), @JsonSubTypes.Type(value = DataB.class, name = "data-b") }) 字段编写自定义序列化程序/反序列化程序,如here所述。