如何运行这个JAXRS程序?我该怎么办?

时间:2015-04-14 08:44:10

标签: maven

我已经花了一个月的时间来运行这个程序,但没有找到任何解决方案。 这是一个复制的程序。我只是想在任何方面运行它。 我想把它当作休息服务。它有什么错误吗?

请帮忙。 这就是我项目中的全部内容。

提前谢谢!!

这是我的计划:

Coustomer.java

package com.abhi.shek.idea.book;

public class Customer {
private int id;
private String firstName;
private String lastName;
private String street;
private String city;
private String state;
private String zip;
private String country;

public int getId() {
    return id;
}

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

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getZip() {
    return zip;
}

public void setZip(String zip) {
    this.zip = zip;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
  }
}

这是我的另一个课程:

CustomerResource.java

package com.abhi.shek.idea.book;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


@Path("/customers")
public class CustomerResource{
    private Map<Integer, Customer> customerDB = new ConcurrentHashMap<Integer, Customer>();
    private AtomicInteger idCounter = new AtomicInteger();

    @POST
    @Path("/create")
    @Consumes("application/xml")
    public Response createCustomer(InputStream is) {
        Customer customer = readCustomer(is);
        customer.setId(idCounter.incrementAndGet());
        customerDB.put(customer.getId(), customer);
        System.out.println("Created customer " + customer.getId());
        return Response.created(
                URI.create("/customers/" + customer.getId())).build();
    }

    @GET
    @Path("{name}")
    @Produces("MediaType.APPLICATION_JSON")
    public StreamingOutput getCustomer(@PathParam("name")String name) {
        final Customer customer = customerDB.get(name);
        if (customer == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        return new StreamingOutput() {
            public void write(OutputStream outputStream)
                    throws IOException, WebApplicationException {
                outputCustomer(outputStream, customer);
            }
        };
    }

    @PUT
    @Path("{id}")
    @Consumes("application/xml")
    public void updateCustomer(@PathParam("id") int id, InputStream is) {
        Customer update = readCustomer(is);
        Customer current = customerDB.get(id);
        if (current == null)
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        current.setFirstName(update.getFirstName());
        current.setLastName(update.getLastName());
        current.setStreet(update.getStreet());
        current.setState(update.getState());
    }

    protected void outputCustomer(OutputStream os, Customer cust)
        throws IOException {
        PrintStream writer = new PrintStream(os);
        writer.println("<customer id=\"" + cust.getId() + "\">");
        writer.println("<first-name>" + cust.getFirstName()
        + "</first-name>");
        writer.println("<last-name>" + cust.getLastName()
        + "</last-name>");
        writer.println("<street>" + cust.getStreet() + "</street>");
        writer.println("<city>" + cust.getCity() + "</city>");
        writer.println("<state>" + cust.getState() + "</state>");
        writer.println("<zip>" + cust.getZip() + "</zip>");
        writer.println("<country>" + cust.getCountry() + "</country>");
        writer.println("</customer>");
        }

    protected Customer readCustomer(InputStream is) {
        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(is);
            Element root = doc.getDocumentElement();
            Customer cust = new Customer();
            if (root.getAttribute("id") != null
            && !root.getAttribute("id").trim().equals("")) {
            cust.setId(Integer.valueOf(root.getAttribute("id")));
            }
            NodeList nodes = root.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            if (element.getTagName().equals("first-name")) {
            cust.setFirstName(element.getTextContent());
            }
            else if (element.getTagName().equals("last-name")) {
            cust.setLastName(element.getTextContent());
            }
            else if (element.getTagName().equals("street")) {
            cust.setStreet(element.getTextContent());
            }
            else if (element.getTagName().equals("city")) {
            cust.setCity(element.getTextContent());
            }
            else if (element.getTagName().equals("state")) {
            cust.setState(element.getTextContent());
            }
            else if (element.getTagName().equals("zip")) {
            cust.setZip(element.getTextContent());
            }
            else if (element.getTagName().equals("country")) {
            cust.setCountry(element.getTextContent());
            }
            }
            return cust;

        } catch (Exception e) {
            throw new WebApplicationException(e,
                    Response.Status.BAD_REQUEST);
        }
    }
}

这是我的第三个项目:

ShoppingApplication.java

package com.abhi.shek.idea.book;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/services")
public class ShoppingApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();

public ShoppingApplication() {
    singletons.add(new CustomerResource());
}

@Override
public Set<Class<?>> getClasses() {
    return empty;
}

@Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

这是我的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    /xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.abhi.shek.idea.book</groupId>
  <artifactId>JAXRS-Book</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>

<dependencies>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.9</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.9</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
    </dependency>

</dependencies>

最后一个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>JAXRS-Book</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> 
  <servlet>
    <servlet-name>com.abhi.shek.idea.book.ShoppingApplication</servlet-name>
    <servlet-class>
                  org.glassfish.jersey.spi.container.servlet.ServletContainer
            </servlet-class>
    <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>com.abhi.shek.idea.book.CustomerResource</param-value>
</init-param>

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>com.abhi.shek.idea.book.ShoppingApplication</servlet-name>
       <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

错误:在控制台上:

SEVERE: Servlet [RestClient] in web application [/JAXRS-RestClient] threw  load() exception
java.lang.ClassNotFoundException:     com.sun.jersey.spi.container.servlet.ServletContainer
at         org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)

另一个错误是:

 com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>    (RootResourceUriRules.java:99)

0 个答案:

没有答案