java - spring mvc 项目返回 406 错误 - 将对象列表序列化为 xml 和 json

标签 java spring-mvc

显示如下406:

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

根据我对问题的理解,我缺乏以 xml 或 json 格式显示数据的转换(也许我在这方面也是错的..)但是我不知道该怎么做...任何帮助都是赞赏!

代码如下:

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
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;

@Controller
@RequestMapping("/userType")
public class UserTypeController {

    private static SessionFactory factory = new Configuration().configure().buildSessionFactory(); 

    /* Method to  READ all the employees */
    @RequestMapping(value = "/list.xml", method = RequestMethod.GET)
    public @ResponseBody List<UserType> listUserTypes( ) {
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            List<UserType> userTypes = (List<UserType>)session.createQuery("FROM UserType").list();
            return userTypes;
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace(); 
            return new ArrayList<UserType>();
        }finally {
            session.close(); 
        }
    }
}

hibernate映射类是:

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;

@XmlRootElement(name = "UserType")
@XmlAccessorType(XmlAccessType.NONE)
public class UserType {
    @XmlElement(name = "UserTypeID")
    private int id;
    @XmlElement(name = "UserTypeName")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Spring 配置包括:

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.pretech" />
    <mvc:annotation-driven />
  </beans>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>SpringRestFulExample</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

使用上面的应用程序,我调用以下网址,它们都返回相同的 406:

http://localhost:8080/SpringRestFulExample/userType/list.xml

http://localhost:8080/SpringRestFulExample/userType/list.json

http://localhost:8080/SpringRestFulExample/userType/list

编辑:

我认为实际上问题是浏览器无法接受列表作为输入;但是这也应该序列化为 xml 或 json,我该怎么做?

最佳答案

您需要使用类似 Jackson 的内容来序列化您的响应或GSON (对于 json),然后添加 @Produces 注释以设置响应中的内容类型 header 。 Jersey and Jax-RS也不错。

关于java - spring mvc 项目返回 406 错误 - 将对象列表序列化为 xml 和 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751929/

相关文章:

java - Spring MVC - 如何在缺少参数时返回 404

java - 请求语法错误 : spring mvc

java - 如何让 "run"按钮自动启动指定配置的项目

java - Gradle 依赖关系树中星号和箭头符号的含义

spring - 命令对象和DTO,有区别吗?

java - 无法为 spring security 提供自定义身份验证提供程序

java - Spring Boot Web应用程序在WEB-INF中找不到图像

java - 当第二次运行时,如何将两个不同的变量放入 recyclerview 中的 TextView 中?

java - 我如何验证它,以便下面的前 3 位数字范围为 100-300(含),后 3 位数字范围为 001-999(含)

java - java中的抽象类