gpt4 book ai didi

java - 尝试使用 REST 生成 JSON 时出现 HTTP 500 错误

转载 作者:行者123 更新时间:2023-12-02 05:13:04 24 4
gpt4 key购买 nike

尝试使用 REST 从数据库条目生成 JSON 代码,但收到 HTTP 500 错误代码


@Path("product")
public class ProductResource {
@GET
@Produces({ MediaType.APPLICATION_XML })
public List<Product> getProductInformation() {
return ProductDAO.instance.getProductInformation();
}
}

^^ 这是我已映射到我尝试访问的资源的类

然后它访问 ProductDAO 类,该类从数据库获取信息并将其添加到 HashMap(这部分似乎工作正常)

public enum ProductDAO {
instance;

private Map<Integer, Product> productMap = new HashMap<Integer, Product>();

private ProductDAO() {
loadFromDb();
}

public Map<Integer, Product> loadFromDb() {
productMap.clear();
try {
Connection conn = Utils.getConnection();
PreparedStatement psmt = conn.prepareStatement("SELECT * FROM products");
ResultSet rs = psmt.executeQuery();
while (rs.next()) {
Product p = new Product(rs.getInt("id"), rs.getString("description"), rs.getInt("CategoryId"),
rs.getDouble("price"), rs.getInt("QuantitySold"), rs.getString("image"));
productMap.put(productMap.size() + 1, p);
System.out.println(productMap);
}
} catch (Exception e) {
e.printStackTrace();
}
return productMap;
}

public List<Product> getProductInformation() {
List<Product> product = new ArrayList<Product>();
product.addAll(productMap.values());
return product;
}

目前我的数据库中有 4 个测试条目。当我为 HashMap 添加 system.out.println() 时,控制台会得到以下输出:

Database connection established
{1=myApp.Product@20c427e5}
{1=myApp.Product@20c427e5, 2=myApp.Product@610e026a}
{1=myApp.Product@20c427e5, 2=myApp.Product@610e026a, 3=myApp.Product@2e0456eb}
{1=myApp.Product@20c427e5, 2=myApp.Product@610e026a, 3=myApp.Product@2e0456eb, 4=myApp.Product@5241c547}`

我的pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.framesinmind</groupId>
<artifactId>FramesIM</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FramesIM</name>

<build>
<finalName>FramesIM</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>myApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

Java 类都是“myApp”包的一部分当尝试访问 url ' http://localhost:8080/FramesIM/rest/product ' 时,我收到 HTTP 500 错误。有什么想法吗?

最佳答案

内部服务器错误是因为您需要使用注释对 Product 类进行编码,并且无法直接将列表作为响应传递。请按照以下代码操作。

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
@XmlRootElement(name = "product")
@XmlAccessorType (XmlAccessType.FIELD)
public class Product {
String id;

public String getId() {
return id;
}

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

}

import java.util.List;

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

@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class Products
{
@XmlElement(name="product")
private List<Product> products;

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}
}


import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("product")
public class ProductResource {
@GET
@Produces(MediaType.APPLICATION_XML)
public Products getProductInformation() {
List<Product> product = ProductDAO.instance.getProductInformation();
Products products = new Products();
products.setProducts(product);
return products;
}
}

下面的响应是示例输出

<products>
<product>
<id>12</id>
</product>
</products>

关于java - 尝试使用 REST 生成 JSON 时出现 HTTP 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314732/

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