2

我正在开发一个 Spring 3.2 MVC 应用程序。它的设置相当典型,但我的问题是当我尝试从控制器方法返回 JSON 对象时收到 406 状态错误。

令人沮丧的部分是,除了我的一种方法之外,我还从服务器获得了响应。我包含的代码是我在网上找到的修改后的 Spring MVC json 示例。带有路径变量的示例控制器方法按预期工作,但我添加到控制器的其他方法没有。

我对示例代码所做的唯一其他更改发生在 pom.xml 中。我将杰克逊依赖升级到最新版本。

那么我错过了什么?我的类路径上有杰克逊映射器罐子。我<mvc:annotation-driven />在我的 servlet 中使用该指令,并且在我的@ResponseBody每个控制器方法中使用注释。

任何帮助将不胜感激!

调度员小服务程序:

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

    <context:component-scan base-package="uk.co.jeeni" />

    <mvc:annotation-driven />

    </beans>

pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>

<groupId>uk.co.jeeni</groupId>
<artifactId>spring-json</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>Spring 3 MVC JSON Example</name>

<properties>
    <spring.version>3.2.1.RELEASE</spring.version>
</properties>

<dependencies>
    <!--
      Jackson mapper is used by spring to convert
      Java POJOs to JSON strings.
    -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.12</version>
    </dependency>

    <!-- START: Spring web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

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

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- END: Spring web -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
        </plugin>
  </plugins>
    <finalName>spring-mvc-json</finalName>
</build>

控制器:

    package uk.co.jeeni;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;


    @Controller
    public class JsonService {

    private static Map<String, Person> data = new HashMap<String, Person>();
    static{
    data.put("ADAM", new Person("Adam", "Davies", 42));
    data.put("JANE", new Person("Jane", "Harrison", 35));
    }

    @RequestMapping(value="{name}", method = RequestMethod.GET)
    public @ResponseBody Person getPerson(@PathVariable String name){
    Person p = new Person(name, name, 105);//data.get(name.toUpperCase());
    return p;
    }


@RequestMapping(value="/testJson.html", method=RequestMethod.GET)
public @ResponseBody List<Person> testJson()
{
    List<Person> people = new ArrayList<Person>();

    for(int i=0;i<10;i++)
    {
        Person p = new Person("Firstname", "Lastname", i);
        people.add(p);
    }

    return people;
}

@ResponseBody
@RequestMapping(value="testJsonPojo.html", method=RequestMethod.GET)
public Person testJsonPojo()
{
    Person individual = new Person("Firstname", "Lastname", 105);

    return individual;
}
}

人.java:

    package uk.co.jeeni;

    public class Person {
    private String firstName;
    private String lastName;
    private int age;

    public Person(String firstName, String lastName, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    }

    public String getFirstName() {
    return firstName;
    }

    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }

    public String getLastName() {
    return lastName;
    }

    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }
}
4

2 回答 2

3

不工作的方法的映射 URL 以 .html 结尾。这没有任何意义,因为您正在返回 JSON 数据。我建议您更改映射,例如:

@RequestMapping(value="/testJson.json", method=RequestMethod.GET)

由于扩展,Spring 将请求的媒体类型设置为 html,因此不会转发到 JSON 视图。

于 2013-05-25T13:15:46.893 回答
1

这可能是您在 3.2 中引入的 mvc:annotations 的问题,此处已得到解答:

https://stackoverflow.com/a/13939290/3014094

在 Spring 3.2 中需要对 mvc:annotations 进行额外的更改。它试图根据请求的扩展自动分配媒体类型。我猜它可能是 /doSomething.html 没有映射到 JSON。

更新您的 mvc spring 配置以关闭此自动映射

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <!-- Turn off working out content type based on URL file extension, should 
        fall back to looking at the Accept headers -->
    <property name="favorPathExtension" value="false" />
</bean>
于 2013-11-21T10:40:31.190 回答