com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:连接被拒绝:连接

时间:2014-04-01 17:45:47

标签: rest jax-rs maven-jetty-plugin jersey-client jetty-8

我一直试图在mvn clean install阶段自动启动和停止Jetty以进行单元测试。令人烦恼的是,当我在JBoss中部署war文件时,所有测试都运行正常,但是,当我尝试使用maven-jetty-plugin时出现Connection refuse: connect错误。我确信Jetty已启动,但无法识别URL,因此无法建立连接。

的pom.xml

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.13.v20130916</version>
            <configuration>
                <webApp>
                    <contextPath>/</contextPath>
                </webApp>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

默认web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
      version="3.0">
</web-app>

和我的其他课程

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.buraktas.resource.DefaultBookResource;

@ApplicationPath("/services")
public class BaseApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();

    public BaseApplication() {

        singletons.add(new DefaultBookResource());
    }

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

实体类

公共类BookEntity {

private int    id;
private String title;
private String author;
private double price;

... setters and getters

}

资源界面

@Path("/book-service")
public interface BookResource {

    @POST
    @Path("/create")
    @Consumes("application/json")
    @Produces("application/json")
    public BookEntity createBook(BookEntity bookEntity);

}

及其实施

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.buraktas.entity.BookEntity;

public class DefaultBookResource implements BookResource {

    private Map<Integer, BookEntity> list      = new HashMap<Integer, BookEntity>();
    private AtomicInteger            idCounter = new AtomicInteger();

    @Override
    public BookEntity createBook(BookEntity bookEntity) {

        bookEntity.setId(idCounter.getAndIncrement());
        list.put(bookEntity.getId(), bookEntity);

        return bookEntity;
    }

}

最后测试课程

public class TestClass {

    private static final String POST_INPUT = "{\"title\" : \"LOTR\",\"author\" : \"J.K.Rowling\",\"price\" : 12.99}";
    private static Client       client;

    @Before
    public void setUp() {

        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        client = Client.create(clientConfig);
    }

    @After
    public void close() {

        client.destroy();
    }

    @Test
    public void testCreateSuccess() throws Exception {

        WebResource webResourcePost = client.resource("http://localhost:8080/jersey-rest-client/services/book-service/create");
        ClientResponse response = webResourcePost.accept("application/json").type("application/json").post(ClientResponse.class, POST_INPUT);
        BookEntity responseEntity = response.getEntity(BookEntity.class);

        assertEquals(200, response.getStatus());
        assertEquals("LOTR", responseEntity.getTitle());
        assertEquals("J.K.Rowling", responseEntity.getAuthor());
        assertEquals(12.99, responseEntity.getPrice(), 0.0001);

    }
}

你也可以从这里找到整个实现

https://github.com/flexelem/jersey-client-example

我会感激每一个回复。不管怎样,谢谢

0 个答案:

没有答案