Java Spring和XML响应
我对SpringMVC和通过@ResponseBody进行的XML响应有问题。 这是我的web.xml:Java Spring和XML响应,java,xml,spring,Java,Xml,Spring,我对SpringMVC和通过@ResponseBody进行的XML响应有问题。 这是我的web.xml: <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>FormEncodingSetterFilter</filter-name>
<filter-class>ua.yura.controllers.EncodingFilter.BKIFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FormEncodingSetterFilter</filter-name>
<url-pattern>*.htm</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>bki</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bki</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
使用注释@XMLRoot和@XMLElement初始化客户端。在我的另一个java应用程序中,我想向DispatcherServlet发送请求,并用客户机对象返回XML数据。我试着做下一步:
URL my = new URL("http://localhost:8080/xml/test.htm");
HttpURLConnection yc = (HttpURLConnection)my.openConnection();
yc.addRequestProperty("accept", "application/xml");
//yc.setRequestMethod("GET");
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream() ));
String inputLine;
while( (inputLine = in.readLine()) != null)
System.out.println( inputLine);
in.close();
但我每次收到错误406时,web浏览器都会显示下一条消息:
此请求标识的资源只能根据请求接受标头生成具有不可接受特征的响应。
有人能帮我纠正错误吗 我认为头应该是Accept not Accept Ref:我也尝试过,但是Tomcat在错误描述中写了请求接受头,我认为问题是另一个。
@Controller
@RequestMapping( value = "/xml" )
public class XMLController {
@RequestMapping( value = "/test.htm", headers = "accept=application/xml")
@ResponseBody
public Client test() {
System.out.println("aaaa");
Client client = new Client(-1,"1","1","1","1","1","1","1");
return client;
}
}
URL my = new URL("http://localhost:8080/xml/test.htm");
HttpURLConnection yc = (HttpURLConnection)my.openConnection();
yc.addRequestProperty("accept", "application/xml");
//yc.setRequestMethod("GET");
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream() ));
String inputLine;
while( (inputLine = in.readLine()) != null)
System.out.println( inputLine);
in.close();