4

背後にある問題を突き止めるのに苦労していSEVERE: A message body writer for Java class java.lang.Boolean, and Java type class java.lang.Boolean, and MIME media type application/json was not foundます。

これが私が試したことです:

  • jersey-bundleこれらの個々のパッケージの代わりに使用する
  • POJOMappingFeature は既に有効になっています
  • Jackson を依存関係として追加する

他に何をする必要があるのか​​ わかりません。これはローカルで問題なく実行されるため、1 つまたは 2 つの依存関係がアプリケーションの残りの部分と一緒にパッケージ化されていない理由は不明です。

pom.xml

    <jersey-version>1.17.1</jersey-version>

...

    <!-- Jersey -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey-version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>${jersey-version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey-version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>${jersey-version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

ジャージーサーブレット

//add jersey servlet support
ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new SpringServlet());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.company.product.resource");
jerseyServletRegistration.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.company.product.resource.ResponseCorsFilter");
jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.feature.DisableWADL", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("org.codehaus.jackson.map.DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY", Boolean.TRUE.toString());
jerseyServletRegistration.setLoadOnStartup(1);

私のビルドプロセス

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>shade</goal></goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.company.product.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>

例外:

SEVERE: A message body writer for Java class java.lang.Boolean, and Java type class java.lang.Boolean, and MIME media type application/json was not found
SEVERE: The registered message body writers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter

エラーを生成するコード:

@Path("/system")
public class SystemResource extends BaseResource {
    @GET
    @Path("/isOnline")
    public Boolean isOnline () {
        return Boolean.TRUE;
    }
}

@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class BaseResource {

}

jersey-json が jar に含まれていることをパッケージ ログで確認しました。私にとって不確かな領域の 1 つは、どのパッケージ/クラスが実際に Boolean のメッセージ本文ライターを提供しているのかということです。

報奨金が追加されたのは、これにより実稼働環境での実行が妨げられており、これを解決する必要があるからです。

4

4 に答える 4

4

jarクラスと他のすべての依存関係を含む1 つの大きなファイルを作成していると仮定します。これが正しい場合 (プロバイダーが 1 つしかない投稿された例外もこの仮定につながります)、依存関係のすべてのjavax.ws.rs.ext.MessageBodyReader(およびjavax.ws.rs.ext.MessageBodyWriter) ファイルが一緒に結合され、追加された最新の jar のファイルに置き換えられるだけではないことを確認してください。ユーバージャー。

javax.ws.rs.ext.MessageBodyWriteruber-jar 内のファイル ( 内META-INF\services) に、 内にあるこのファイルのプロバイダーが含まれていることを確認してくださいjersey-json

com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$Wadl
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

POJOMappingFeatureJSONプロバイダーとしてJacksonを使用しているため(に設定することにより) 、最後のものはあなたの場合に特に重要ですtrue

編集1

Jersey 1.x の多くのモジュールは、META-INF/servicesメカニズムを使用して JAX-RS 提供 ( MessageBodyReaders など) を検索します。jersey-json.jar!META-INF/services/javax.ws.rs.ext.MessageBodyReaderつまり、Jersey ランタイムが使用するクラスが定義されている (つまり、入力/出力との間でエンティティを読み書きするために)サービス ファイルを jar ファイル (つまり ) で見つけることができます。uber-jar を作成する際の問題は、同じサービスのコンテンツ (依存関係にある同じ名前のファイルによって定義されているMETA-INF/services/) を 1 つのファイルに結合し、それを実行可能 jar にバンドルする必要があることです (つまり、結合META-INF/services/javax.ws.rs.ext.MessageBodyReaderjersey-corejersey-json保存する)。META-INF/services/javax.ws.rs.ext.MessageBodyReaderあなたの瓶に入れます)。

ジャージーの場合、jersey-bundle他の場所で述べたように使用できますが、META-INF/servicesファイルjersey-bundleが結果の jar にあることを確認してください。

于 2013-10-15T12:50:39.910 に答える
1

ジャクソンも含めるようにしてください:

<jackson.version>1.9.12</jackson.version>
...
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>${jackson.version}</version>
    </dependency>
于 2013-10-10T13:59:32.460 に答える
0

jersey-json パッケージに含まれているため、サードパーティのライブラリを追加する必要はありません。コードは正しくないようです。次のようにする必要があります。

@Path("/system")
public class SystemResource extends BaseResource {
    @GET
    @Path("/isOnline")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    public Boolean isOnline () {
        return Boolean.TRUE;
    }
}

public class BaseResource {

}
于 2013-10-15T02:31:19.140 に答える