我有春天的cxf restFul集成:

rest-context.xml

   <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
        xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
        xsi:schemaLocation="
                http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

        <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />


        <cxf:bus>
            <cxf:features>
                <cxf:logging />
            </cxf:features>
        </cxf:bus>
        <bean id="userManager" class="com.restWS.impl.UserManager" />
        <jaxrs:server id="userManagerREST" address="/rest/UserManager">
            <jaxrs:serviceBeans>
                <ref bean="userManager" />
            </jaxrs:serviceBeans>
            <jaxrs:providers>
                <ref bean='jsonProvider' />
            </jaxrs:providers>
        </jaxrs:server>
    </beans>


web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    metadata-complete="true">

    <display-name>RestDemo</display-name>
    <description>RestDemo</description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/rest-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
</web-app>


UserManager.java

package com.restWS.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.model.Personel;
import com.restWS.IUserManager;

@Path("/personelService")
@Consumes("application/json")
@Produces("text/plain")
public class UserManager implements IUserManager {
    public UserManager() {
        System.out.println("horaaaaaaaaaaaaaaaaaaaa the object is initioalized nowwwwwwwwwwwwwwwwwwww");
    }

    @GET
    @Path("/hello/{personCode}")
    public Personel getPersonelByPersonCode(@PathParam("personCode") String personCode) {
        Personel personel = new Personel(personCode);
        return personel;
    }
}


IUserManager.java

package com.restWS;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.model.Personel;

@Path("/personelService")
@Consumes("application/json")
@Produces("text/plain")
public interface IUserManager {
    @GET
    @Path("/hello")
    public Personel getPersonelByPersonCode(String personCode);
}


personel.java

package com.model;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.cxf.aegis.type.java5.XmlElement;
import org.springframework.beans.factory.annotation.Autowired;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Personel")
public class Personel implements Serializable {
    private String nationalCode;
    private String name;
    private String lastName;
    private String fatherName;

    public Personel(String personCode) {
        this.name = "reza";
        this.lastName = "ramezani matin";
        this.fatherName = "ali";
        this.nationalCode = "3980020827";
    }

    public String getNationalCode() {
        return nationalCode;
    }

    public void setNationalCode(String nationalCode) {
        this.nationalCode = nationalCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

}


以上项目已成功部署在weblogic 12.1.1中。
firefox发送的请求为“ http://localhost:7001/restfulWS-0.0.1-SNAPSHOT/services/rest/UserManager/personelService/hello/10”的请求时,有效负载:找不到响应类Personel的消息正文编写器,已在控制台中登录

Content-Type: text/plain
Headers: {Date=[Wed, 22 Apr 2015 06:12:26 GMT]}
Payload: No message body writer has been found for response class Personel.

最佳答案

您需要告诉服务器该Web方法将生成xml输出,您可以通过添加以下行来做到这一点,尝试通过删除类级别产生标签

 @GET
 @Path("/hello/{personCode}")
 @Produces("application/xml") //added this line ,you can declare anything json/xml
 public Personel getPersonelByPersonCode(@PathParam("personCode") String personCode) {
     Personel personel = new Personel(personCode);
     return personel;
 }


您还需要使用xml标记注释您的Personnel类

@XmlElement(name="nationalCode")
private String nationalCode;

@XmlElement(name="name")
private String name;

@XmlElement(name="lastName")
private String lastName;

@XmlElement(name="fatherName")
private String fatherName;


注释可以跳过,但是它使您能够自定义标签(例如,“ firstName”可以用作“ fName”标签)

希望这可以帮助!

祝好运!

10-07 15:45