java - Spring Webservices 给出 406 错误

标签 java spring web-services spring-mvc

我已经创建了一个带有 webservices 的简单 Spring 示例,但是当我尝试获取响应时,我收到错误如下:

HTTP 状态 406 -

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

这是我的 Spring Controller :

@Controller
public class DataController {

    @RequestMapping("studentlist")
    public @ResponseBody
    List<Student> getStudentList() {
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student(2, "A1", "B1", "a@gmail.com", "123456"));
        studentList.add(new Student(3, "A2", "B2", "b@gmail.com", "123456"));
        studentList.add(new Student(4, "A3", "B3", "c@gmail.com", "123456"));

        return studentList;
    }
}

这是我的 Student.java 文件:

@XmlRootElement
public class Student {

    int id;
    String firstName;
    String lastName;
    String email;
    String mobileNumber;

// Setters & Getters
}

我的 Spring Web 配置文件具有以下条目:

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

    <mvc:annotation-driven />

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="text/xml" />                
            <entry key="htm" value="text/html" />
        </map>
    </property>
    <property name="defaultContentType" value="text/html" />
      <property name="defaultViews">
    <list>
      <!-- JSON View -->
      <bean
        class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
      </bean>

      <!-- JAXB XML View -->
      <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg>
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
               <property name="classesToBeBound">
                <list>
                   <value>com.web.domain.Student</value>
                </list>
               </property>
            </bean>
        </constructor-arg>
      </bean>
     </list>
  </property>
  <property name="ignoreAcceptHeader" value="true" />    

</bean>

根据此设置,我了解如果我的 URL 以 .json 结尾,我将获得 JSON 响应。如果它以 .xml 结尾,我将得到 XML 响应。同样,如果 URL 以 .htm 结尾,我将得到一个 html 响应。

如果我点击的 URL 为 http://localhost:8080/spring-ws/studentlist.json 那么我将成功获得 JSON 响应。

现在,如果我的 URL http://localhost:8080/spring-ws/studentlist.xmlhttp://localhost:8080/spring-ws/studentlist.htm,然后我收到以下错误消息:

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

您能帮我解决这个问题吗?

另外,我的代码中的 ignoreAcceptHeader 属性有什么用?总是需要这样做吗?

我引用了这篇文章:Spring RESTful Web Service with JSON gives HTTP 406 error code

正如上面的帖子中给出的,我已经尝试在提交时将 Accept header 设置为 application/xmltext/html使用 Chrome PostMan 客户端请求,但我仍然看到同样的问题。

最佳答案

您正在尝试在 Controller 中返回 Student 对象的 List

因此,创建一个带有 JAXB 注释的包装器类,并在从 Controller 返回时使用该包装器类来解决问题。

例如,创建这样的类:

@XmlRootElement
public class WrapperList<T> {

    private List<T> list;

    public WrapperList() {
        list = new ArrayList<T>();
    }

    public WrapperList(List<T> list) {
        this.list = list;
    }

    @XmlAnyElement
    public List<T> getItems() {
        return list;
    }
}

并将其返回到您的类(class)中:

@RequestMapping("studentlist")
    public 
    WrapperList<Student> getStudentList() {
        List<Student> studentList = new ArrayList<Student>();
        // add students to the list and put them in wrapper class 
        WrapperList<Student> list = new WrapperList<Student>(studentList);
        return list;
    }

您还可以像这样保持配置简单:

 <beans>
    <context:component-scan base-package="com.web.controller" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json" />
                <entry key="xml" value="text/xml" />
                <entry key="htm" value="text/html" />
            </map>
        </property>
        <property name="defaultContentType" value="text/html" />
        <property name="ignoreAcceptHeader" value="true" />
    </bean>
</beans>

关于java - Spring Webservices 给出 406 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30946880/

相关文章:

java - maven-shade-plugin ConnectException : Connection refused for getting java. sun.com DTD(由 XMLAppendingTransformer 处理以构建 Apache CXF)

java - 如何使用来自网络服务的列表填充 Tapestry5 调色板?

Java最佳实践,在对象修改之前还是之后添加到集合?

Java,如何比较字符串和字符串数组

java - 为什么ByteBuffer.allocate()和ByteBuffer.allocateDirect()之间的奇异性能曲线差异

java - spring JdbcTemplate 是如何识别数据类型的?

java - 使用 Joda-time 以 GMT 格式保存日期 - Java

Spring Integration 作为独立 ESB 的嵌入式替代方案

web-services - 带有格式不正确的 WSDL 的 JAXB 自定义

java - Axis 网络服务错误