在restfulcxf中没有找到响应正文编写器的响应

时间:2015-04-22 06:29:03

标签: java rest weblogic

我有春天的cxf restFul inegeration:

其余-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”时,Payload:没有为响应类找到消息正文编写器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.

1 个答案:

答案 0 :(得分:0)

你需要告诉服务器这个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;
可以跳过

注释,但它可以让您自定义标签(例如,#34; firstName&#34;可以用作&#34; fName&#34;标签)

希望这有帮助!

祝你好运!