0

すべてのフォーム データを収集し、XML としてコントローラーに送信しようとしています。この XML はさらにバックエンドに送信され、処理が行われます。
JAXBMarshaller は、着信 xml をマーシャリングするために Bean が定義されていることを想定しています。しかし、私はそれを持っていません。
リクエスト:

$('form').submit(function () {
                    $.ajax({
                        url: $(this).attr('action'),
                        type: 'POST',
                        processData: false,
                        data: collectFormData1(),
                        headers: {
                            "Content-Type":"application/xml"
                        },
                        dataType: 'application/xml',
                        success: function (data) {
                            alert('Success:'+data)
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            console.log('jqXHR:'+jqXHR+'\n'+'textStatus:'+'\n'+textStatus+'errorThrown:'+errorThrown);
                        }
                    });

                    return false;
                });
function collectFormData1()
            {
                //$rootElement = $('<FormXMLDoxument/>');
                xmlDoc = document.implementation.createDocument("", "", null);
                root = xmlDoc.createElement($('form').attr('name'));
                $('form').find('div.section').each(function(index, section) {
                    sectionElement = xmlDoc.createElement($(section).attr('name'));
                    //xmlDoc.appendChild(sectionElement);
                    $(section).find('input').each(function(i, field) {
                        fieldElement  = xmlDoc.createElement($(field).attr('name'));
                        fieldText=xmlDoc.createTextNode($(field).val());
                        fieldElement.appendChild(fieldText);
                        sectionElement.appendChild(fieldElement);
                    });
                    root.appendChild(sectionElement);
                });
                xmlDoc.appendChild(root);
                console.log((new XMLSerializer()).serializeToString(xmlDoc));
                return xmlDoc;                  
            }

コントローラ

@RequestMapping(value="/save",method=RequestMethod.POST,consumes={"application/json", "application/xml", "text/xml"})
        @ResponseBody public String handleSave(@RequestBody String formData)
        {

            System.out.println("comes here");
            System.out.println(formData);//prints the form xml
return "<response>Success</response>";

    } 
4

1 に答える 1

1

これは、xml をマーシャリング/アンマーシャリングできるHttpMessageConverterがないことが原因である可能性が非常に高いです。

spring-oxmまだ行っていない場合は追加します。

クラスパススキャンを使用している場合は、コンポーネントでも使用@EnableWebMvcします。 そうでない場合は、構成を追加して、デフォルトのコンバーターを有効にします。@Configuration
<mvc:annotation-driven/>

http://hillert.blogspot.com/2011/01/rest-with-spring-contentnegotiatingview.html

アップデート


に追加produces={"application/xml"}してみてください@RequestMapping

応答本文として返す必要がある単純な xml 文字列の場合、Spring MVC のフォーム ポストから単純な xml 文字列を返す方法またはResponseEntity<String>xml を含む を返す方法で説明されているように、HttpServletResponse.getWriter() を使用できます。

@RequestMapping(value="/save",method=RequestMethod.POST,consumes={"application/json", "application/xml", "text/xml"})
@ResponseBody public ResponseEntity<String> handleSave(@RequestBody String formData)
{

    System.out.println("comes here");
    System.out.println(formData);//prints the form xml

    return new ResponseEntity<String>("<response>Success</response>", HttpStatus.OK);
}
于 2013-08-12T13:58:45.627 に答える