有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

尝试使用REST生成JSON时发生java HTTP 500错误

试图使用rest从数据库条目生成JSON代码,但我得到了一个HTTP 500错误代码


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

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

然后它访问ProductDAO类,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个测试条目。 当我添加一个系统时。出来println()对于HashMap,我将向控制台获得以下输出:

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>

网络。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错误。有什么想法吗


共 (0) 个答案