簡體   English   中英

球衣測試框架灰熊返回404 not found錯誤

[英]jersey-test-framework grizzly returns 404 not found error

我想在我的應用程序中實現針對Jersey REST / Json的JUnit測試。

我的問題是我沒有從Grizzly找回404。 但是路徑是正確的,並且可以在普通Webcontainer上使用Curl進行測試。

我正在使用GWTP(沒有maven)。 我正在手動實現屬性-> Java Build Path。 我正在使用以下版本:

  • Eclipse Indigo(在Windows 7上)
  • ASM-3.3.1.jar
  • 新澤西州的客戶 - 1.12.jar
  • 新澤西州的核心1.12.jar
  • 新澤西州JSON-1.12.jar
  • 新澤西服務器1.12.jar
  • 新澤西州的servlet-1.12.jar
  • 球衣測試框架 - 核心1.12.jar
  • 球衣測試框架,灰熊,1.12.jar
  • 灰熊框架,1.9.45.jar
  • Grizzly的HTTP-1.9.45.jar
  • Grizzly的HTTP-servlet的1.9.45.jar
  • 灰熊-portunif-1.9.45.jar
  • 灰熊-RCM-1.9.45.jar
  • 灰熊-servlet的Web服務器,1.9.45.jar
  • 灰熊-utils的-1.9.45.jar
  • servlet的API-2.5.jar
  • junit-4.9.jar(還與4.10.jar一起測試)

根據Jersey文檔提供了所有庫: http : //jersey.java.net/nonav/documentation/latest/user-guide.html-第7.5章在Maven外部運行測試。

以下是堆棧跟蹤:

INFO: GRIZZLY0001: Starting Grizzly Framework 1.9.45 - 01.07.12 12:11
01.07.2012 10:11:20 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.acolsolutions.loyaltycard.resources
01.07.2012 10:11:20 com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class com.acolsolutions.loyaltycard.resources.HelloResource
class com.acolsolutions.loyaltycard.resources.CardResource
01.07.2012 10:11:20 com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
01.07.2012 10:11:20 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM'
com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:9998   /api/cards returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.voidHandle(WebResource.java:707)
at com.sun.jersey.api.client.WebResource.access$400(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:553)
at   com.acolsolutions.loyaltycard.junit.CardResourceTests.testCreate(CardResourceTests.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
01.07.2012 10:11:21    com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory$GrizzlyWebTestContainer stop
INFO: Stopping the Grizzly Web Container...

我的課程如下所示:

CardResourceTests.java:

import static org.junit.Assert.assertFalse;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest; 
import com.sun.jersey.test.framework.WebAppDescriptor;

import org.codehaus.jettison.json.JSONObject;

public class CardResourceTests extends JerseyTest {
private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig()); 


public CardResourceTests() throws Exception {
super("com.acolsolutions.loyaltycard.resources");
}
/*  
@Before
public void setUp() {
helper.setUp();
}

@After
public void tearDown() {
helper.tearDown();
}
*/  
@Test
public void testCreate() {
boolean thrown = false;
WebResource webResource = resource();
JSONObject card = new JSONObject(); 

try {
card.put("id", "1")
.put("name", "Name of Card")
.put("code", "123456")
.put("codeTypeId", "1")
.put("cardProviderName", "The Card Provider")
.put("picturePath", "provider.jpg")
.put("cardProviderUrl", "http://www.provider.com")
.put("creationDate", "Sun Jun 10 08:55:14 UTC 2012")
.put("modificationDate","Sun Jun 10 08:55:14 UTC 2012");
webResource.path("api/cards").type("application/json").post(card);
} catch(Exception e) {
e.printStackTrace();
thrown = true;
}
assertFalse(thrown);        
}
}

CardRessource.java:

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
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.core.MediaType;
import javax.ws.rs.core.Response;

import com.acolsolutions.loyaltycard.dataobjects.CardDAO;
import com.acolsolutions.loyaltycard.persistence.entities.Card;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.allen_sauer.gwt.log.client.Log;

@Path("/cards")
public class CardResource {
CardDAO dao = new CardDAO();

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(Card card) {
Log.debug("Creating card");
Card c = dao.create(card);
if(c.equals(null)) {
return Response.status(400).entity("Create failed!").build();
}
return Response.status(201).entity(c).build();
}
...
}

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_2_5.xsd" version="2.5">

<display-name>loyaltycard</display-name>

<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>loyaltycard.html</welcome-file>
</welcome-file-list>

<!-- 
The below lines are implementing Jersey (JAX-RS) in to the GWT application.
-->
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>       
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.acolsolutions.loyaltycard.resources</param-value>
</init-param>        
</servlet>

<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

<!--
This Guice listener hijacks all further filters and servlets. Extra
filters and servlets have to be configured in your
ServletModule#configureServlets() by calling
serve(String).with(Class<? extends HttpServlet>) and
filter(String).through(Class<? extends Filter)
-->
<listener>
<listener-class>com.acolsolutions.loyaltycard.server.guice.GuiceServletConfig</listener-class>
</listener>

<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

當將應用程序作為Web Appliction運行時,使用Curl和以下命令進行測試可以工作:

curl http://localhost:8888/api/cards -i -X POST -H "Content-Type: application/json" -d "{\"id\": \"1\", \"name\": \"Name of Card\", \"code\": \"123456\", \"codeTypeId\": \"1\", \"cardProviderName\": \"Card Provider\", \"picturePath\": \"provider.jpg\", \"cardProviderUrl\": \"http://www.provider.com\", \"creationDate\": \"Sun Jun 10 08:55:14 UTC 2012\", \"modificationDate\": \"Sun Jun 10 08:55:14 UTC 2012\" }"

我什至嘗試用RawCap.exe嗅探回送接口。 我在PCAP文件中看到以下內容:

POST /api/cards HTTP/1.1
Content-Type: application/json
User-Agent: Java/1.7.0
Host: localhost:9998
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 254

{"id":"1","name":"Name of Card","code":"123456","codeTypeId":"1","cardProviderName":"Card Provider","picturePath":"provider.jpg","cardProviderUrl":"http:\/\/www.provider.com","creationDate":"Sun Jun 10 08:55:14 UTC 2012","modificationDate":"Sun Jun 10 08:55:14 UTC 2012"}

HTTP/1.1 404 Not Found

server: grizzly/1.9.45
Content-Type: text/plain; charset=iso-8859-1
Transfer-Encoding: chunked
Date: Sun, 01 Jul 2012 00:02:16 GMT

在我看來,JSON字符串已提交到URL,但是找不到任何類。

我被卡在這里了一點。 我的上下文路徑一定做錯了,但是我不知道該怎么做以及如何設置它。

克里斯,謝謝你的幫助

當將應用程序作為Web應用程序運行時,它會基於web.xml在/ api / URL處公開。 Grizzly或輕量級HTTP服務器都無法識別web.xml,因此它們在容器根目錄公開jersey。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM