首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jaxb2Marshaller的UnMarshalling content-type=application/x-www-form-urlencoded抛出错误“prolog中不允许内容”

使用Jaxb2Marshaller的UnMarshalling content-type=application/x-www-form-urlencoded抛出错误“prolog中不允许内容”
EN

Stack Overflow用户
提问于 2013-04-19 19:24:52
回答 1查看 2.9K关注 0票数 0

我正在尝试设置一个rest服务来处理content-type=application/x-www-form-urlencoded.的请求我目前正在使用Jaxb2Marshaller对请求进行解组。然而,在解组时,它抛出错误"org.xml.sax.SAXParseException: Prolog.中不允许内容“。

我将xml请求作为字符串进行检查。它的url编码形式为:%3C%3Fxml+version=%221.0%22+encoding%3D%22UTF-8%22+standalone%3D%22yes%22%3F%3E%3Cxrsi%.

似乎这个编码的xml字符串请求导致了这个错误。除了解组之外,还有什么方法可以先解码请求吗?

下面是我的上下文设置:

代码语言:javascript
复制
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000000" />
    </bean>

    <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="autodetectAnnotations" value="true" />
        <!-- Set some properties to make the outputted xml look nicer -->
    </bean>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- Configure the XStream message converter -->
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="jaxbMarshaller2" />
                <property name="unmarshaller" ref="jaxbMarshaller2" />

                <property name="supportedMediaTypes">
                    <list>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg index="0" value="application" />
                            <constructor-arg index="1" value="xml" />
                        </bean>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg index="0" value="application" />
                            <constructor-arg index="1" value="x-www-form-urlencoded" />
                        </bean>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <bean id="jaxbMarshaller2" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

        <property name="classesToBeBound">
            <list>
                <value>com.auto.server.schema.ReceiveRequest</value>
                <value>com.auto.server.schema.ReceiveReply</value>

            </list>

        </property>
    </bean>

    <bean name="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

        <property name="ignoreAcceptHeader" value="true" />
        <property name="favorParameter" value="true" />
        <property name="favorPathExtension" value="true" />
        <!-- if no content type is specified, return json. -->
        <property name="defaultContentType" value="application/x-www-form-urlencoded" />

        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/x-www-form-urlencoded" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <constructor-arg ref="jaxbMarshaller" />
                    <property name="modelKey" value="responseObject" />
                </bean>
            </list>
        </property>
    </bean>

    <!-- REST API controllers -->
    <context:component-scan base-package="com.auto.server.schema" />

Here is the controller part

@ResponseBody
    @RequestMapping(value = "/heartbeat", method = RequestMethod.POST, headers = { "content-type=application/x-www-form-urlencoded" }, consumes = "application/x-www-form-urlencoded;charset=UTF-8")
    public Object heartBeat(@RequestBody ReceiveRequest request) {

        ReceiveReply reply = new ReceiveReply();        
        return reply;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-08 22:40:03

如果您总是要以URL编码的方式获取数据,即content-type="application/x-www-form-urlencoded"

然后,当您通过以下代码接收到它时,您可以在bean中简单地解码它:

代码语言:javascript
复制
String decodedString = java.net.URLDecoder( inputString, "UTF-8" );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16103506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档