2013-10-15 32 views
1

我在我的REST應用程序中使用了Jersey。我需要用junit來測試它。但我得到了一個com.sun.jersey.api.client.ClientHandlerException。 Stacktrace:com.sun.jersey.api.client.ClientHandlerException

SEVERE: The registered message body readers compatible with the MIME media type are: 
application/octet-stream -> 
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider 
com.sun.jersey.core.impl.provider.entity.FileProvider 
com.sun.jersey.core.impl.provider.entity.InputStreamProvider 
com.sun.jersey.core.impl.provider.entity.DataSourceProvider 
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider 
*/* -> 
com.sun.jersey.core.impl.provider.entity.FormProvider 
com.sun.jersey.core.impl.provider.entity.StringProvider 
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider 
com.sun.jersey.core.impl.provider.entity.FileProvider 
com.sun.jersey.core.impl.provider.entity.InputStreamProvider 
com.sun.jersey.core.impl.provider.entity.DataSourceProvider 
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General 
com.sun.jersey.core.impl.provider.entity.ReaderProvider 
com.sun.jersey.core.impl.provider.entity.DocumentProvider 
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader 
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader 
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader 
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General 
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General 
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General 
com.sun.jersey.core.impl.provider.entity.EntityHolderReader 

com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class  com.example.Student, and Java type class com.example.Student, and MIME media type application/octet-stream was not found 
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561) 
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517) 
at com.example.servlet.StudentResourceTest.test(StudentResourseTest.java:85) 
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:47) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
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) 

我不知道如何解決這個錯誤。我期待着您的幫助。 我的代碼:

@Path("/student") 
@Component 
public class StudentResourse { 
@Autowired 
private StrudentService service; 

@Path("/getStudent/{id}") 
@GET 
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
public Student getStudent(@PathParam("id") long id) { 
    return service.get(id); 
} 
} 

此課程正常工作。當我在瀏覽器中鍵入:http://127.0.0.1:8080/Application/student/getStudent/100500它讓我看到學生XML 我的測試類:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:configuration.xml") 
@TestExecutionListeners({ DbUnitTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class, 
    DirtiesContextTestExecutionListener.class, 
    TransactionalTestExecutionListener.class }) 
public class StudentResourseTest extends JerseyTest { 

@Override 
protected AppDescriptor configure() { 
    return new WebAppDescriptor.Builder("com.example.servlet") 
      .contextPath("context").build(); 
} 

@Override 
protected TestContainerFactory getTestContainerFactory() { 
    return new HTTPContainerFactory(); 
} 

@Test 
@DatabaseSetup("dataset.xml") 
public void test() throws UnsupportedEncodingException { 
     ClientConfig config = new DefaultClientConfig(); 
     Client client = Client.create(config); 
     WebResource service = client.resource(getBaseURI()); 
     ClientResponse response = service.path("getStudent").path("100500") 
       .accept(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON) 
       .get(ClientResponse.class); 
     Student student = (Student) response.getEntity(Student.class); 
     System.out.println(student); 
} 

@Override 
public URI getBaseURI() { 
    return UriBuilder.fromUri("http://127.0.0.1:8080/Application/student/") 
      .build(); 
} 

}

我使用這種依賴關係:

<dependency> 
    <groupId>com.sun.jersey.jersey-test-framework</groupId> 
    <artifactId>jersey-test-framework-http</artifactId> 
    <version>1.2</version> 
</dependency> 

Student類標記爲@XmlRootElement 。問題在測試課上。有任何想法嗎?感謝您的解答

+0

你在1.2版本中使用,甚至新澤西后?你需要使用這樣一箇舊版本,或者你可以切換到最新的1.17.1版本嗎?接收到的響應的HTTP狀態是什麼? –

+0

它返回404 Not Found。我不知道爲什麼,因爲它在瀏覽器中工作 –

回答

1

您正在將應用程序部署到context上下文路徑,但是您假定(通過重寫getBaseURI()方法)應用程序部署在Application上下文路徑中。嘗試刪除壓倒一切的getBaseURI()方法並更改測試方法如下:

@Test 
@DatabaseSetup("dataset.xml") 
public void test() throws UnsupportedEncodingException { 
    ClientResponse response = resource() 
     .path("students").path("getStudent").path("100500") 
     .accept(MediaType.APPLICATION_XML) 
     .get(ClientResponse.class); 

    Student student = (Student) response.getEntity(Student.class); 
    System.out.println(student); 
} 

同時檢查HTTP響應狀態(應該是200)和響應媒體類型(應該是在這種情況下application/xml - 如果你要求application/xml並得到application/octet-stream東西顯然是錯的)。使用

1

下面註釋球衣

@XmlRootElement 

變化庫 和服務器日誌乾淨發佈項目

相關問題