Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey没有看到我的MessageBodyReader_Java_Jersey_Jax Rs - Fatal编程技术网

Java Jersey没有看到我的MessageBodyReader

Java Jersey没有看到我的MessageBodyReader,java,jersey,jax-rs,Java,Jersey,Jax Rs,我试图将jersey与我自己的json MessageBodyReader/MessageBodyWriter一起使用(因为我没有在我的域类上使用@XmlRootElement…注释) 我得到的错误如下所示: A message body reader for Java class test.Greeting, and Java type class test.Greeting, and MIME media type application/json was not found 我想知道编写

我试图将jersey与我自己的json MessageBodyReader/MessageBodyWriter一起使用(因为我没有在我的域类上使用@XmlRootElement…注释)

我得到的错误如下所示:

A message body reader for Java class test.Greeting, and Java type class test.Greeting, and MIME media type application/json was not found

我想知道编写自己的MessageBodyReader需要什么魔法?

我不确定这是否是您的情况,但常见的错误是
isReadable
方法的实现不正确

你实施了吗?
调试时,您是否在此停止?

它返回真值吗?

过了一会儿,我找到了问题的根本原因。我对MessageBodyReader/Writer的实现还可以(我认为它可以与RESTlet配合使用),但是如果您使用JerseyTest,请不要忘记将MessageBodyReader/Writer添加到它的ClientConfig中:

/**
 * Creates custom REST client config which is mandatory since we don't use any JSON providers.
 * @return Jersey Client Config with the required classes to read/write in(out)coming data.
 */
private static ClientConfig createClientConfig() {
    final ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(GsonMessageBodyHandler.class);
    config.getClasses().add(GsonAwareContextResolver.class);
    return config;
}

/**
 * Public ctor
 * @throws com.sun.jersey.test.framework.spi.container.TestContainerException On error
 */
public MyRestExposureTest() throws TestContainerException {
    super(new WebAppDescriptor.Builder("my.rest.package")
            .clientConfig(createClientConfig())
            .contextPath("/")
            .build());
}

否则,您的客户端代码将无法读取/写入POJO。

这就是我正在使用的,它正在工作(目前使用Jersey 1.8)


在尝试了Alex的解决方案后,这终于对我起了作用:

public IntegrationTest() throws TestContainerException {
    super(new LowLevelAppDescriptor.Builder(createResourceConfig())
                  .build());
}

private static ResourceConfig createResourceConfig() {
    ResourceConfig rc = new PackagesResourceConfig("com.github.joschi.jersey.security.smime");
    rc.getSingletons().add(new EnvelopedWriter());
    rc.getSingletons().add(new SignedWriter());
    return rc;
}

我使用的是jersey 1.17,但这对我不起作用。永远不会计算客户端配置。
/**
 * Creates custom REST client config which is mandatory since we don't use any JSON providers.
 * @return Jersey Client Config with the required classes to read/write in(out)coming data.
 */
private static ClientConfig createClientConfig() {
    final ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(GsonMessageBodyHandler.class);
    config.getClasses().add(GsonAwareContextResolver.class);
    return config;
}

/**
 * Public ctor
 * @throws com.sun.jersey.test.framework.spi.container.TestContainerException On error
 */
public MyRestExposureTest() throws TestContainerException {
    super(new WebAppDescriptor.Builder("my.rest.package")
            .clientConfig(createClientConfig())
            .contextPath("/")
            .build());
}
public static Client createMyClient() {
    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(MyProviderClass1.class);
    cc.getClasses().add(MyProviderClass2.class);
    cc.getClasses().add(MyProviderClass3.class);
    return Client.create(cc);
}
public IntegrationTest() throws TestContainerException {
    super(new LowLevelAppDescriptor.Builder(createResourceConfig())
                  .build());
}

private static ResourceConfig createResourceConfig() {
    ResourceConfig rc = new PackagesResourceConfig("com.github.joschi.jersey.security.smime");
    rc.getSingletons().add(new EnvelopedWriter());
    rc.getSingletons().add(new SignedWriter());
    return rc;
}