使用CXF开发RESTFul接口,接口泛型参数转json的问题

发布于 2021-11-24 02:38:28 字数 5306 浏览 819 评论 2

CXF版本 2.7.4,Json provider 使用的org.codehaus.jackson,版本1.9.9

现在遇到的问题:每次调用父接口方法时都会报如下错误,请问有人知道怎么解决吗?

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.io.Serializable, problem: abstract types can only be instantiated with additional type information
 at [Source: {"employee_ID":null,"employee_No":"aaa","employee_Name":"aaa"}; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:233)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:60)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1196)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1144)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:695)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:655)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:238)
... 33 more

代码如下:

public interface ParentWebService<V extends Serializable> {
	
	@POST
	@Path("/add/")
	@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
	Response insert(V vo) throws ApplicationException;
	
	@PUT
	@Path("/update/")
	@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
	Response update(V vo) throws ApplicationException;
	
	@DELETE
	@Path("/delete/{id}")
	@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
	Response delete(@PathParam("id") String id) throws ApplicationException;
}
public abstract class ParentWebServiceImpl<V extends Serializable> implements ParentWebService<V> {

	protected abstract ParentApplication<V> getApplication();
	
	@Override
	public Response insert(V vo) {
		int result = getApplication().add(vo);
		return Response.ok(result).build();
	}

	@Override
	public Response update(V vo) {
		int result = getApplication().modify(vo);
		return Response.ok(result).build();
	}

	@Override
	public Response delete(String id) {
		getApplication().delete(id);
		return Response.ok().build();
	}

}
@Produces( { MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })
@Path("/emp")
public interface EmployeeWebService extends CommonWebService<EmployeeVO> {
	
} /* 实现类比较简单,省略,只实现ParentWebServiceImpl.getApplication() */
@XmlRootElement(name = "EmployeeVO")
@JsonIgnoreProperties(ignoreUnknown=true)
public class EmployeeVO implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer employee_ID;

    private String employee_No;

    private String employee_Name;

    /* getter setter 省略 */
}

cxf 配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://cxf.apache.org/transports/http/configuration"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://cxf.apache.org/transports/http/configuration 
	http://cxf.apache.org/schemas/configuration/http-conf.xsd
	http://www.springframework.org/schema/util 
	http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/jaxrs
	http://cxf.apache.org/schemas/jaxrs.xsd
	http://cxf.apache.org/core
	http://cxf.apache.org/schemas/core.xsd"
	default-autowire="byName">

	<!-- all application configure should configure in this file -->

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

	<!-- JSON PROVIDER -->
	<bean id="jacksonJaxbJsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
	<bean id="jacksonJsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />

	<cxf:bus>
		<cxf:features>
			<bean class="org.apache.cxf.transport.common.gzip.GZIPFeature" />
		</cxf:features>
	</cxf:bus>

	<http:conduit name="*.http-conduit">
		<http:client AllowChunking="false" ConnectionTimeout="60000" />
	</http:conduit>

	<jaxrs:server id="server" address="/services">
		<jaxrs:serviceBeans>
			<ref bean="employeeWebService" />
		</jaxrs:serviceBeans>
		<jaxrs:providers>
			<ref bean="jacksonJsonProvider" />
		</jaxrs:providers>
	</jaxrs:server>

</beans>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2021-11-27 07:58:28

谢谢先。 感觉不是Null的问题,我现在把ParentWebService改为ParentWebService<V>,去掉了Serializable,程序就正常了,不算真正解决,暂时先这么做了

别再吹冷风 2021-11-24 17:36:16

你这里有个Null值,泛类型就不能正确解析它,因为不知道具体要实现为什么类型,所有会出错。如果知道具体格式,最好是用固定类去解析json。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文