简体   繁体   English

在restfulcxf中未找到响应的消息正文编写器

[英]No message body writer has been found for response in restfulcxf

i have cxf restFul inegeration of spring: 我有春天的CXF restFul集成:

rest-context.xml 休息-的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 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 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 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 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;
    }

}

the above project is deployed in weblogic 12.1.1 successfully. 以上项目已成功部署在weblogic 12.1.1中。 when is sended request by firefox that the request is " http://localhost:7001/restfulWS-0.0.1-SNAPSHOT/services/rest/UserManager/personelService/hello/10 ",Payload: No message body writer has been found for response class Personel is logged in console 当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.

you need to tell the server that this webmethod will generate an xml output you can do that by adding this line,try by removing the class level produces tag 您需要告诉服务器该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;
 }

you also need to annotate your Personnel class with xml tags like this 您还需要使用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;

annotation can be skipped ,but it gives you power to customize tags (Eg "firstName" can be used as "fName" tag) 注释可以跳过,但是它使您能够自定义标签(例如,“ firstName”可以用作“ fName”标签)

hope this helps! 希望这可以帮助!

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到响应类ArrayList [jboss 7]的消息正文编写器 - No message body writer has been found for response class ArrayList [jboss 7] 未找到响应类 ArrayList 的消息正文编写器 - No message body writer has been found for response class ArrayList 访问服务时,未找到响应类别的消息正文编写器 - No message body writer has been found for response class when accessing the service 在osgi中使用带有cxf的jackson提供程序(未找到消息正文编写器) - Using jackson provider with cxf in osgi (no message body writer has been found) 严重:未找到 class java.util.Vector 的消息正文编写器,ContentType:application/xml - SEVERE: No message body writer has been found for class java.util.Vector, ContentType: application/xml Apache CXF作为Restful API的客户端-找不到该类的消息正文编写器 - Apache CXF as Client for restful API - No message body writer has been found for class 没有为MultipartBody,multipart / form-data找到消息正文编写器 - No message body writer has been found for MultipartBody, multipart/form-data 找不到Java类的消息正文编写器 - A message body writer for Java class not found CXF-找不到类附件的消息正文阅读器 - CXF - no message body reader has been found for class Attachment 使用泽西(Jersey)开机自检以服务-“未找到消息正文编写器...” - Using Jersey to POST to service - “A message body writer … was not found”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM