gpt4 book ai didi

java - Spring MVC 使用 ContentNegotiationManagerFactoryBean 仅返回 json 406

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:25 25 4
gpt4 key购买 nike

我希望仅针对向 http://localhost:8090/home/home/home2 发出的任何请求(将 servlet 上下文设置为“home”)转换 JSON 响应。

如果没有接受 header ,我设置的配置是返回默认值“JSON”响应。我还尝试将接受类型设置为“application/json”,但这不起作用。

解决方案位于Spring MVC + JSON = 406 Not Acceptable当我尝试通过 ContentNegotiationManagerFactoryBean 类时,使用了 Messageconverter 的不同方法。

web.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">

<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

servlet-context.xml如下。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.andy.main" />


<resources mapping="/resources/**" location="/resources/" />



<beans:bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<beans:property name="ignoreAcceptHeader" value="true"/>
<beans:property name="defaultContentType" value="application/json" />

<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="json" value="application/json" />
<beans:entry key="xml" value="application/xml" />
</beans:map>
</beans:property>
</beans:bean>

<annotation-driven content-negotiation-manager="contentNegotiationManager" />


</beans:beans>

java代码如下:

package com.andy.main;

import java.util.HashMap;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Handles requests for the application home page.
*/
@Controller
@RequestMapping("/home")
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/home2" , method = RequestMethod.GET)
public @ResponseBody HashMap<String, String> home(Locale locale, Model model) {
System.out.println("In Home2");
logger.info("Welcome home! The client locale is {}.", locale);
HashMap<String, String> allValues=new HashMap<String, String>();
allValues.put("Doctor","Dr.John");
allValues.put("Location", "location1");
return allValues;
}

}

最佳答案

如果您使用@ResponseBody或@RequestBody,则意味着Spring Message Conversions默认情况下,该机制将被激活,并且您的 ContentNegotiationManager 无法获得控制权。

但是为什么 MappingJackson2HttpMessageConverter (或之前版本的 MappingJacksonHttpMessageConverter)不起作用?我猜你忘记添加 jackson 库:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>

关于java - Spring MVC 使用 ContentNegotiationManagerFactoryBean 仅返回 json 406,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27669568/

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