我正在尝试设置一个处理 content-type=application/x-www-form-urlencoded 请求的休息服务。我目前正在使用 Jaxb2Marshaller 来解组请求。然而,在解组时,它会抛出错误“[org.xml.sax.SAXParseException: Content is not allowed in prolog.]”。
我将 xml 请求检查为字符串。它的 url 编码形式为:%3C%3Fxml+version=%221.0%22+encoding%3D%22UTF-8%22+standalone%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;
}