使用HTML表单的SpringMVC Jackson多态反序列化

时间:2015-07-01 22:11:20

标签: java json eclipse spring-mvc jackson

您好我正在使用带有@JsonTypeInfo和@JSonSubTypes注释的jackson多态反序列化:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,include=JsonTypeInfo.As.PROPERTY,property="type")
@JsonSubTypes({
        @Type(value = CustomerFileContent.class, name="FileContent"),
        @Type(value = CustomerContactContent.class, name="ContactContent"),
        @Type(value = CustomerTextContent.class, name="TextContent"),
        @Type(value = CustomerWebContent.class, name="WebContent")
})

public class CustomerContent
{
    private int categoryId_;
    private int contentId_;
    private String contentName_;
    private String contentType_;
    private int contentOrder_;
    private double contentVersion_;
    private int contentStatus_;
    private int moveOldVal_;
    private int moveNewVal_;
    protected FinarAppDataSource dataSource;


@JsonTypeName("FileContent")
public class CustomerFileContent extends CustomerContent 
{
    private int fileContentId_;
    private String filePath_;
    private String fileType_;

当我从客户端发送数据作为Json对象时,它工作正常。 但是,当我尝试从html表单发送这些数据时(如下面的代码所示),它不会将我的基类类型对象(CustomerContent)强制转换为Concrete类类型对象(CustomerFileContent)。

<form action="<%= blobstoreService.createUploadUrl("/customercontentmain") %>" name="contentForm" onSubmit="disableSubmitButton()" method="POST" enctype="multipart/form-data">
    <table cellspacing="1" cellpadding="0">
        <tr><td>İçerik İsmi :  </td> <td><input type="text" name="contentName" id="contentName" required/></td></tr>
        <tr><td>İçerik Dosyası : </td> <td><input type="file" name="filePath" required/></td></tr>     
    <br>
    <tr>
        <td><input type="hidden" id="categoryId" name="categoryId" value="${categoryid}"/>
            <input type="hidden" id="contentStatus" name="contentStatus" value="<%= FinarConstants.mode_insertVal %>"/>
            <input type="hidden" id="contentType" name="contentType" value="<%= FinarConstants.content_type_file %>" />
            <input type="hidden" id="fileType" name="fileType" value="pdf" />
            <input type="hidden" id="contentId" name="contentId" value="-1"/>
            <input type="hidden" id="moveOldVal" name="moveOldVal" value="-1"/>
            <input type="hidden" id="moveNewVal" name="moveNewVal" value="-1"/>
            <input type="hidden" id="type" name="type" value="FileContent" /></td> 
        <td><input type="submit" id="submitButton" value="Ekle...." /></td>
    </tr>
    </table>
</form>

这是我的spring-mvc接收部分

@RequestMapping(value="/customercontentmain", method = RequestMethod.POST)
public String getCustomerContentMainpage(@ModelAttribute("contentInput") CustomerContent contentInput)
{
    String result = "FinarCustomerLoginError";
    if (request.getSession().getAttribute("customerid") == null)
    {
        result = "Finar";
    } else{

        Map<String, List<BlobKey>> blobs = blobStoreService.getUploads(request);
        BlobKey filePathKey  = blobs.get("filePath").get(0);


        //FinarCustomerContent content = contentInput.getContent();
        contentInput.setContentFilePath(filePathKey.getKeyString());
        CustomerContentController contentController = new CustomerContentController(contentInput);
        contentController.setContentData();
        result = contentController.getContentPage();
    }       

    return result;
}

有没有人知道这个问题的解决方案? 或者这是杰克逊的一个错误,我应该使用像angular.js这样的东西吗?

由于 托加

0 个答案:

没有答案