首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >东猫7号泽西岛休息不工作

东猫7号泽西岛休息不工作
EN

Stack Overflow用户
提问于 2014-11-27 17:37:55
回答 2查看 2.6K关注 0票数 2

我跟随了无数的例子和教程,试图让我的REST web服务运行,但没有任何成功。我使用的是IntelliJ 13、Maven3、Java7和Tomcat 7。

我的pom.xml看起来是这样的:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<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.jafwatt</groupId>
    <artifactId>rest-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0-m10</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.18</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

我的web.xml看起来是这样的:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <display-name>rest</display-name>
    <servlet>
        <servlet-name>restServlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.jafwatt.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>restServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

CustomerResource.java

代码语言:javascript
复制
package com.jafwatt.rest;

import com.jafwatt.rest.entity.Customer;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Path("/customers")
public class CustomerResource {

    private Map<Integer, Customer> customerDB = new ConcurrentHashMap<Integer, Customer>();
    private AtomicInteger idCounter = new AtomicInteger();

    @GET
    @Path("/hello")
    @Produces("text/plain")
    public String getCustomers() {
        return "Hello";
    }

    @POST
    @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("{id}")
    @Produces("application/xml")
    public StreamingOutput getCustomer(@PathParam("id") int id) {
        final Customer customer = customerDB.get(id);
        if (customer == null) {
            throw new WebApplicationException(
                    Response.Status.NOT_FOUND);
        }
        return new StreamingOutput() {
            public void write(OutputStream outputStream)
                    throws IOException, WebApplicationException {
                outputCustomer(outputStream, customer);
            }

            private void outputCustomer(OutputStream outputStream, Customer 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());
        current.setZip(update.getZip());
        current.setCountry(update.getCountry());
    }

    private Customer readCustomer(InputStream is) {
        return new Customer();
    }
}

Maven构建没有错误的WAR,Tomcat部署没有错误的WAR,但是除了404响应之外,其他任何URL组合都不会得到。

根上下文是'rest‘,我尝试过的URL:

代码语言:javascript
复制
http://localhost:8090/rest
http://localhost:8090/rest/custsomers
http://localhost:8090/rest/custsomers/hello

我知道Tomcat正在那个端口上运行,因为我可以访问manager应用程序。

我读过很多关于Stackoverflow和其他地方的文章,我真的很沮丧,因为应该是一个相对简单的任务是我无法完成的。

请帮帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-29 20:48:03

问题是我的依赖性。

这就是他们现在的样子--我的web服务正在工作:

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.18.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.18.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.18.1</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
票数 2
EN

Stack Overflow用户

发布于 2014-11-29 04:34:22

尝试以下几点:

在intellij项目设置中,确保在模块下正确设置了Facet ' Web‘-Web资源目录。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27176313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档