UnMarshalling content-type=application/x-www-form-urlencoding using Jaxb2Marshaller 会抛出错误"Content is



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

我检查了xml请求为字符串。以url编码形式表示:%3C%3Fxml+version=%221.0%22+encoding%3D%22UTF-8%22+standalone%3D%22yes%22%3F%3E%3Cxrsi%

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

下面是我的上下文设置:
<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;
    }

如果你总是想以url编码的方式获取数据例如content-type="application/x-www-form-urlencoded"

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

String decodedString = java.net.URLDecoder( inputString, "UTF-8" );

相关内容

最新更新