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

时间:2019-05-26 15:11:59

标签: java rest http

试图使用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错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

内部服务器错误是因为您需要使用注释对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>