Java com.sun.jersey.api.client.ClientHandlerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19379013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
com.sun.jersey.api.client.ClientHandlerException
提问by Oleksandr H
I'm using Jersey in my REST-application. I need to test it with junit. But I got an com.sun.jersey.api.client.ClientHandlerException
. Stacktrace:
我在我的 REST 应用程序中使用 Jersey。我需要用junit测试它。但我得到了一个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.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.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access@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);
}
}
0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.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)
I do not know how to fix this bug. I'm looking forward to your help. My code:
我不知道如何修复这个错误。我期待着你的帮助。我的代码:
@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();
}
This class works fine. When I type in browser: http://127.0.0.1:8080/Application/student/getStudent/100500
it shows me student in xml
My test class:
这个类工作正常。当我在浏览器中输入时:http://127.0.0.1:8080/Application/student/getStudent/100500
它以 xml 格式显示我的学生我的测试类:
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-http</artifactId>
<version>1.2</version>
</dependency>
}
}
I'm using this dependency:
我正在使用此依赖项:
@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);
}
Student class marked as @XmlRootElement. Problem is in test class. Any ideas? Thanks for you answers
学生类标记为@XmlRootElement。问题出在测试课上。有任何想法吗?谢谢你的回答
采纳答案by Michal Gajdos
You're deploying your application to context
context-path but then you're assuming (by overriding getBaseURI()
method) that the application is deployed on Application
context-path. Try to remove the overriding getBaseURI()
method and change the test method to following:
您将应用程序部署到context
上下文路径,但随后您假设(通过覆盖getBaseURI()
方法)应用程序部署在Application
上下文路径上。尝试删除覆盖getBaseURI()
方法并将测试方法更改为以下内容:
Also check for HTTP response status (should be 200
) and response media-type (should be application/xml
in this case - if you request application/xml
and get application/octet-stream
something is clearly wrong).
还要检查 HTTP 响应状态(应该是200
)和响应媒体类型(application/xml
在这种情况下应该是- 如果您请求application/xml
并得到application/octet-stream
某些东西显然是错误的)。
回答by Anil
Please check A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found
请检查未找到 Java 类型、myPackage.B 类和 MIME 媒体类型应用程序/八位字节流的消息正文编写器
& this linkto resolve your issue.I hope it is enough to sort out your problem.
&这个链接可以解决您的问题。我希望它足以解决您的问题。
回答by gajera bhavin
using below annortation @XmlRootElement and change library of jersey AND server log clean after publish the project
发布项目后使用以下注释@XmlRootElement 并更改球衣库和服务器日志清理