在Apache CXF中没有很好地配置json提供程序

时间:2014-03-13 16:01:45

标签: xml json maven jaxb cxf

大家好我在Apache CXF 3.0.0-mileones2中面对json支持的噩梦 我通过浏览器调用Web服务后收到此消息:

13 mars 2014 15:45:54 org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem
GRAVE: No message body writer has been found for class java.util.ArrayList, ContentType:     
application/json

我查找了 org.apache.cxf.jaxrs.utils.JAXRSUtils 类,然后查找了logMessageHandlerProblem方法,该方法的主体如下所示:

    private static Object readFromMessageBody(Class<?> targetTypeClass,
                                              Type parameterType,
                                              Annotation[] parameterAnnotations,
                                              InputStream is, 
                                              MediaType contentType, 
                                              OperationResourceInfo ori,
                                              Message m) throws IOException, WebApplicationException {

    List<MediaType> types = JAXRSUtils.intersectMimeTypes(ori.getConsumeTypes(), contentType);

    final ProviderFactory pf = ServerProviderFactory.getInstance(m);
    for (MediaType type : types) { 
        List<ReaderInterceptor> readers = pf.createMessageBodyReaderInterceptor(
                                     targetTypeClass,
                                     parameterType,
                                     parameterAnnotations,
                                     type,
                                     m,
                                     true,
                                     ori.getNameBindings());
        if (readers != null) {
            try {
                return readFromMessageBodyReader(readers, 
                                                 targetTypeClass, 
                                                 parameterType, 
                                                 parameterAnnotations, 
                                                 is, 
                                                 type,
                                                 m);    
            } catch (IOException e) {
                if (e.getClass().getName().equals(NO_CONTENT_EXCEPTION)) {
                    throw ExceptionUtils.toBadRequestException(e, null);    
                } else {
                    throw e;
                }
            } catch (WebApplicationException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new Fault(ex);
            }
        }
    }

    logMessageHandlerProblem("NO_MSG_READER", targetTypeClass, contentType);
    throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
}

方法 logMessageHandlerProblem 已被调用,这意味着 readers 变量为null,因为如果不是该函数将返回一个类型或抛出以下异常之一: IOException WebApplicationException

这是Web服务接口:

package com.webservices;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.dto.ProductDTO;

@Path("products")
public interface ProductWebService {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("all")
public List<ProductDTO> getAllProducts();
}

顺便说一下,如果我更改为XMl类型:@Produces(MediaType.APPLICATION_XML)而不是@Produces(MediaType.APPLICATION_JSON),它可以正常工作。 我确信我在maven依赖项中缺少一些包,但我添加了所有jaxb和包,我怀疑他们负责转换和出价。

的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>net.mycopany</groupId>
<artifactId>WebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>WebApp Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
    <cxf.version>3.0.0-milestone2</cxf.version>
    <org.springframework.version>3.2.6.RELEASE</org.springframework.version>
</properties>
<build>
    <finalName>WebApp</finalName>
</build>
<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.0-RC1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-service-description</artifactId>
        <version>${cxf.version}</version>
    </dependency>


    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
    </dependency>


    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-bindings-xml</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-databinding-jibx</artifactId>
        <version>${cxf.version}</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.7</version>
    </dependency>
</dependencies>
</project>

感谢您的帮助

0 个答案:

没有答案