【问题标题】:Java Restful API..The requested resource is not availableJava Restful API..请求的资源不可用
【发布时间】:2016-02-11 11:55:37
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: java spring api rest


    【解决方案1】:

    您需要包含实现 JAX-RS API 即 jersey jar 来构建 restful 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的更多详细信息

    【讨论】:

    • 我已经按照您的指示进行操作,但仍然无法正常工作...谁能帮帮我??
    • 您是否已将 servlet 条目添加到 web.xml。您需要将 jersey.config.server.provider.packages 参数设置为您的包名称“productWS”。在此处共享 web.xml 条目,您会遇到错误。
    • 服务器向我发送相同的消息“请求的资源不可用。”
    • 在此处共享 web.xml 条目。
    • 在这里查看 web.xml 的更新pastebin.com/4uCtQJ9B你也可以在这里提出问题。 expertwebindia.com/support
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    相关资源
    最近更新 更多