Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jaxb2Marshaller对content type=application/x-www-form-urlencoded进行解组时抛出错误“;prolog中不允许包含内容;_Java_Spring_Rest_Unmarshalling - Fatal编程技术网

Java 使用Jaxb2Marshaller对content type=application/x-www-form-urlencoded进行解组时抛出错误“;prolog中不允许包含内容;

Java 使用Jaxb2Marshaller对content type=application/x-www-form-urlencoded进行解组时抛出错误“;prolog中不允许包含内容;,java,spring,rest,unmarshalling,Java,Spring,Rest,Unmarshalling,我正在尝试设置一个rest服务来处理content type=application/x-www-form-urlencoded的请求。我目前正在使用Jaxb2Marshaller来解组请求。但是,在解组时,它会抛出错误“[org.xml.sax.SAXParseException:prolog中不允许包含内容。]” 我将xml请求检查为字符串。它的url编码形式为:%3C%3Fxml+版本=%221.0%22+编码%3D%22UTF-8%22+独立%3D%22yes%22%3F%3E%3Cxr

我正在尝试设置一个rest服务来处理content type=application/x-www-form-urlencoded的请求。我目前正在使用Jaxb2Marshaller来解组请求。但是,在解组时,它会抛出错误“[org.xml.sax.SAXParseException:prolog中不允许包含内容。]”

我将xml请求检查为字符串。它的url编码形式为:%3C%3Fxml+版本=%221.0%22+编码%3D%22UTF-8%22+独立%3D%22yes%22%3F%3E%3Cxrsi%

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

以下是我的上下文设置:

<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;
    }

com.auto.server.schema.ReceiveRequest
com.auto.server.schema.ReceiveReply
这是控制器部分
@应答器
@RequestMapping(value=“/heartbeat”,method=RequestMethod.POST,headers={“content type=application/x-www-form-urlencoded”},consumes=“application/x-www-form-urlencoded;charset=UTF-8”)
公共对象检测信号(@RequestBody ReceiveRequest request){
接收方回复=新接收方();
回复;
}

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

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

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

我不明白为什么要使用Jaxb映射表单数据?似乎是个奇怪的要求!与其强迫JAXB理解表单编码的数据,不如在映射到表单的POJO中接收表单值。客户端将发送一个rest调用application-x-www-form-urlencoded。