Java Restful API ..请求的资源不可用

时间:2016-02-11 11:55:37

标签: java spring api rest

我正在尝试使用Java Restful Api ..但是当我运行应用程序时,服务器发回资源未找到..我努力但我无法解决这个问题。我使用Eclipse IDE和Tomcat 7服务器。

................ 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RestfullAndAngularJSTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

........... Product.java文件.....................

package entities;

import java.io.*;
import javax.xml.bind.annotation.*;;

@XmlRootElement(name = "product")
public class Product implements Serializable {
    private String id;
    private String name;
    private double price;

    @XmlElement
    public String getId() {
        return id;
    }

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

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Product(String id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Product() {

    }

}

........... ApplicationConfig.java文件....................

package productWS;

import java.util.Set;

import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(productWS.ProductRestFull.class);
    }

}

......... ProductRestFull.java文件......

package productWS;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import java.util.*;
import entities.*;

@Path("product")
public class ProductRestFull {
    @GET
    @Path("findall")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Product> findAll() {
        List<Product> result = new ArrayList<Product>();
        result.add(new Product("P01", "Name1", 1000.00));
        result.add(new Product("P02", "Name2", 1050.00));
        result.add(new Product("P01", "Name3", 2000.00));
        result.add(new Product("P01", "Name4", 3000.00));
        return result;
    }
}

这是网址:http://localhost:8080/RestfullAndAngularJSTest/rest/product/findall

1 个答案:

答案 0 :(得分:0)

你需要包含实现JAX-RS API,即jersey jar来构建宁静的web服务。

从下面下载球衣罐。

https://jersey.java.net/download.html

将以下servlet条目添加到 web.xml

<servlet>  
        <servlet-name>Jersey REST Service</servlet-name>  
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
        <init-param>  
            <param-name>jersey.config.server.provider.packages</param-name>  
            <param-value>com.expertwebindia.rest</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
      </servlet>  
      <servlet-mapping>  
        <servlet-name>Jersey REST Service</servlet-name>  
        <url-pattern>/rest/*</url-pattern>  
      </servlet-mapping>  

您可以查看有关JAX-RS tutorial

的更多详细信息