gpt4 book ai didi

java - 在使用 String 以外的东西时使用 Spring MVC、Jackson 和 REST 406。想要复杂的类型

转载 作者:行者123 更新时间:2023-11-30 08:30:04 24 4
gpt4 key购买 nike

我的问题是我想使用 Spring MVC 及其附带的标准注释将 Java 集合作为 JSON 返回。不幸的是,我每次都以 HTTP 错误代码 406 告终。

"The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."

我认为 Jackson 和 Spring 可以自动转换我的返回类型,但如果我们使用集合或除 String 之外的任何其他返回类型,它会返回 HTTP 错误 406。你能帮我看看我做错了什么吗?


研究
正如我提到的,我们可以获得一个 String 返回。我们认为问题要么出在 Maven(与 Jackson 的依赖关系)中,要么出在我们对 Spring MVC REST 注释的理解中。另一个原因是我们没有正确配置 Spring 上下文。


Controller
这是我带注释的 Spring Controller 。第一个简单方法 ( getAJsonString) 有效,但第二个 ( getImprovedJsonMap) 带有 map 返回 HTTP 错误代码 406,我们认为这是由于复杂类型造成的:

@Controller
@RequestMapping("/kfc/brands")
public class DefaultMetricsService {

@RequestMapping(value="/firstMethod", method = RequestMethod.GET)
public @ResponseBody String getAJsonString() {
return "HELLO";
}

@RequestMapping(value = "/Metrics/type3", method = RequestMethod.GET)
public @ResponseBody Map<String, String> getImprovedJsonMap() {
Map jsonMap = new HashMap<String, String>();
jsonMap.put("counter", "3000");
return jsonMap;
}


Spring Servlet 定义
这是我的 Spring 配置 ( metrics-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:component-scan base-package="com.t2" />

<mvc:annotation-driven />


</beans>
...


依赖性
这是我的 Jackson 版本的 pom:

<!-- Dependency Versions -->
<org.slf4j.version>1.6.1</org.slf4j.version>
<spring.version>3.2.2.RELEASE</spring.version>
<commons.loggin.version>1.1.1</commons.loggin.version>
<junit.version>4.8.2</junit.version>
<jackson.version>1.9.10</jackson.version>

...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons.loggin.version}</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
...


Servlet
这是我的 servlet 定义:

    <servlet>
<servlet-name>metrics</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:metrics-application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>metrics</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:metrics-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

最佳答案

我认为您可能对 Spring 3.2 中的内容协商有疑问。参见 Spring documentation

尝试在您的 metrics-servlet.xml 中禁用它

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>

关于java - 在使用 String 以外的东西时使用 Spring MVC、Jackson 和 REST 406。想要复杂的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18890266/

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