错误:org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= text / xml的MessageBodyReader

时间:2016-08-23 13:58:29

标签: rest maven jersey jax-rs maven-assembly-plugin

String url = "<url>"

ClientConfig clientConfig = new ClientConfig().register(LoggingFeature.class);
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(ClientProperties.PROXY_URI, "http://localhost:3128");

Client client = ClientBuilder.newClient(clientConfig);

WebTarget webTarget = client.target(url).path("<path>");
webTarget = webTarget.queryParam("<param>", "<value>");

Invocation.Builder iBuilder = webTarget.request(MediaType.APPLICATION_XML_TYPE);
Response response = iBuilder.get();

if(response.getStatus() == 200)
    <JaxbClass> jaxbObj = response.readEntity(<JaxbClass>.class);

使用eclipse执行时,该程序运行正常。 但是在执行jar时会出错。

错误是由行<JaxbClass> jaxbObj = response.readEntity(<JaxbClass>.class);

引起的

使用“JAXBContext”和“Unmarshaller”进行解组,它在执行jar时工作正常。

我正在使用“maven-assembly-plugin”创建包含所有依赖项的jar。

用于创建jar的命令是:mvn package

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo.rest.client</groupId>
    <artifactId>restClient</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>restClient</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-apache-connector</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.demo.rest.client.RestClient</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <properties>
        <jersey.version>2.23.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Maven dependancies

0 个答案:

没有答案