gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:38:32 30 4
gpt4 key购买 nike

我的 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com