在 restfulcxf 中没有找到响应的消息正文编写器
No message body writer has been found for response in restfulcxf
我有 cxf restFul 生成 spring:
休息-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;
}
}
我UserManager.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”,有效负载:未找到响应的消息正文编写器class 人员已登录控制台
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 输出你可以通过添加这一行来做到这一点,尝试通过删除 class 级别生成标签
@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 标签来注释您的人员 class
@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"标签)
希望对您有所帮助!
祝你好运!
我有 cxf restFul 生成 spring:
休息-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;
}
}
我UserManager.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”,有效负载:未找到响应的消息正文编写器class 人员已登录控制台
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 输出你可以通过添加这一行来做到这一点,尝试通过删除 class 级别生成标签
@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 标签来注释您的人员 class
@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"标签)
希望对您有所帮助!
祝你好运!