javascript - jspring 4.0x json响应错误,根据请求 "accept" header ,特征 Not Acceptable

标签 javascript jquery spring

我的 Controller :

        @RestController
public class ClawerController {

    @RequestMapping("/hello")
    public String hello(){        
        return "hello";
    }

	@RequestMapping(value="getNewsByCategoryId", method = RequestMethod.GET)
	public List<News> getNewsByCategoryId(String categoryId) {
		List<News> newsList = new ArrayList<News>();
		try {
			Document doc = Jsoup.connect("http://news.cqu.edu.cn/news/article/list.php?catid="+categoryId).get();
			Elements liphoto = doc.select("div.liphoto div.row1");
			for (Element row : liphoto) {
				Element link = row.select("a").first();
				Element img = row.select("img").first();
				News news = new News(link.attr("href"), img.attr("alt"), "", "http://news.cqu.edu.cn/"+img.attr("src"));
				newsList.add(news);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newsList;
	}
}

我的 web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>project03</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>


<servlet>
 <servlet-name>rest</servlet-name>
 <servlet-class>
  org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

我的 Spring 配置文件:

<p>
  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<context:component-scan base-package="org.echo.spider.controller" />
	<mvc:annotation-driven />
	<mvc:default-servlet-handler/>
</beans>
</p>

我使用以下方式访问我的 Controller :

http://localhost:8080/project03/getNewsByCategoryId?categoryId=46

然后我得到的响应是406:

HTTP Status 406 -
type Status report
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

我使用以下方式访问我的 Controller :

http://localhost:8080/project03/hello

回复是正确的你好。 我怎么了?提前谢谢。

最佳答案

我已经解决了我的问题。它不起作用的原因是我的 Controller 返回了列表或对象数据。
1.我应该将 jackson-annotations、jackson-core 和 jackson-databind jar 添加到我的类路径中。
2.之后,我将 servlet-context.xml 更改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
	<mvc:annotation-driven />
	<context:component-scan base-package="org.echo.spider.controller" />
	
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>

	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
	</bean>

 </beans>

没关系。

关于javascript - jspring 4.0x json响应错误,根据请求 "accept" header ,特征 Not Acceptable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34051154/

相关文章:

javascript - 为什么我不能使用 javascript 和 jquery 读取文本文件?

javascript - 服务后和更新时 Angular 4 CLI 太慢

javascript - ng-click 不在指令内工作

java - 如何在 Spring MVC 中根据用户输入创建新的 url

spring - .rpm 、 '.swi' 和 '.swix' 的常见 mime 类型应该是什么?

java - Spring Integration 从 1.0.x 迁移到 2.2.6

javascript - 如何找到屏幕的中心?

javascript - 在 jquery 中迭代数组并打印 li 元素的文本

javascript - Json 响应未定义

php - jquery错误NS_ERROR_XPC_BAD_CONVERT_JS : in FF works but not on Chrome