gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:33:30 25 4
gpt4 key购买 nike

显示如下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/

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