Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC项目上返回406错误-将对象列表序列化为xml和json_Java_Spring Mvc - Fatal编程技术网

Java 在SpringMVC项目上返回406错误-将对象列表序列化为xml和json

Java 在SpringMVC项目上返回406错误-将对象列表序列化为xml和json,java,spring-mvc,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格式的数据显示转换(可能我在这方面也错了…),但是我不知道如何才能做到这一点。。。感谢您的帮助 代码如下所示:

将显示以下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(); 
        }
    }
}
弹簧配置包括:

<?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>
编辑:


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

您需要使用类似或(对于json)的东西来序列化响应,然后添加@products注释以设置响应中的内容类型标题。你也很好

我认为问题在于你的浏览器不知道如何显示列表。@DwB你说得对!但是,我如何将其合并到代码中,比如使其能够显示?@DwB我还希望允许服务器返回json,目前只允许xml。有什么建议吗?非常感谢。下面是一个使用Spring编写RESTWeb服务的教程。
<?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>
<?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>
http://localhost:8080/SpringRestFulExample/userType/list.xml

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

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