【发布时间】:2012-02-14 22:09:05
【问题描述】:
我是 apache cxf 的新手,我已经使用这项技术开发了一个 Web 服务。
XmlSchema-1.4.2.jar、commons-logging-1.1.1.jar、cxf-2.1.3.jar、geronimo-activation_1.1_spec-1.0.2.jar、geronimo-annotation_1.0_spec-1.1.1 .jar、geronimo-javamail_1.4_spec-1.3.jar、geronimo-servlet_2.5_spec-1.2.jar、geronimo-ws-metadata_2.0_spec-1.1.2.jar、jaxb-api-2.1.jar、jaxb-impl-2.1 .7.jar、neethi-2.0.4.jar、saaj-api-1.3.jar、saaj-impl-1.3.2.jar、wsdl4j-1.6.2.jar、xml-resolver-1.2.jar
这是界面:
@WebService
public interface StoreAdmin
{
boolean logIn(@WebParam(name="legajo") String legajo
,@WebParam(name="lat") String lat
,@WebParam(name="lon") String lon) throws StoreException;
boolean logOut(@WebParam(name="legajo") String legajo) throws StoreException;
boolean addItemToSystem(@WebParam(name="isbn") String isbn
,@WebParam(name="descripcion") String descripcion
,@WebParam(name="precio") double precio
,@WebParam(name="numeroTotal") int numeroTotal) throws StoreException;
@WebResult(name="item")
com.dosideas.cxf.Item getItem(@WebParam(name="isbn") String isbn) throws StoreException;
}
这是实现文件:
package com.dosideas.cxf;
import com.dosideas.cxf.exception.StoreException;
import com.dosideas.cxf.service.ServicioEncriptacion;
import com.dosideas.cxf.service.ServicioItems;
import com.dosideas.cxf.service.ServicioUsuarios;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
/**
*
* @author alonso
*/
@WebService(endpointInterface = "com.dosideas.cxf.StoreAdmin",serviceName="storeAdmin")
public class StoreAdminImpl implements StoreAdmin{
//injectd by spring
private ServicioUsuarios servicioUsuarios;
private ServicioEncriptacion servicioEncriptacion;
private ServicioItems servicioItems;
//constructor...
StoreAdminImpl(ServicioUsuarios _servicioUsuarios
, ServicioEncriptacion _servicioEncriptacion
,ServicioItems _servicioItems)
{
this.servicioUsuarios=_servicioUsuarios;
this.servicioEncriptacion=_servicioEncriptacion;
this.servicioItems=_servicioItems;
}
...
// there are more methods but I don't put here, not relevant
public Item getItem(String isbn) throws StoreException {
return servicioItems.getItem(isbn);
}
public ServicioUsuarios getServicioUsuarios()
{
return servicioUsuarios;
}
}
ServicioItemsImpl.getItem 方法:
hashMapItems 是一个并发HashMap
public Item getItem(String isbn) throws StoreException
{
Logger.getLogger(ServicioItemsImpl.class.getName()).log(Level.INFO, "ServicioItemsImpl. INIT getItem.");
Item item = (Item ) ServicioItemsImpl.hashMapItems.get(isbn);
if (item==null)
{
Logger.getLogger(ServicioItemsImpl.class.getName()).log(
Level.WARNING, "ServicioItemsImpl. METHOD: getItem. ERROR. el item con isbn: {0} no existe en el sistema.", isbn);
//throw new StoreException("27",isbn,null);
}
else
Logger.getLogger(ServicioItemsImpl.class.getName()).log(
Level.INFO, "ServicioItemsImpl. END isbn´s Item: {0}",item.getIsbn());
return item;
}
applicationContext.xml:
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- Declaramos el bean implementación del Servicio Web. Como vemos, es
un bean más de Spring, por lo que podemos inyectarle dependencias,
interceptores, y demás.
-->
<bean id="holaMundoImpl" class="com.dosideas.cxf.HolaMundoImpl"/>
<bean id="servicioUtiles" class="com.dosideas.cxf.service.ServicioUtilesImpl"/>
<bean id="servicioUsuarios" class="com.dosideas.cxf.service.ServicioUsuariosImpl">
<constructor-arg ref="servicioUtiles"/>
<constructor-arg ref="servicioEncriptacion"/>
</bean>
<bean id="servicioEncriptacion" class="com.dosideas.cxf.service.ServicioEncriptacionImpl"/>
<bean id="servicioItems" class="com.dosideas.cxf.service.ServicioItemsImpl"/>
<bean id="servicioApuntesContables" class="com.dosideas.cxf.service.ServicioApuntesContablesImpl"/>
<bean id="storeImpl" class="com.dosideas.cxf.StoreImpl">
<constructor-arg ref="servicioUsuarios"/>
<constructor-arg ref="servicioEncriptacion"/>
<constructor-arg ref="servicioItems"/>
<constructor-arg ref="servicioApuntesContables"/>
</bean>
<bean id="storeAdminImpl" class="com.dosideas.cxf.StoreAdminImpl">
<constructor-arg ref="servicioUsuarios"/>
<constructor-arg ref="servicioEncriptacion"/>
<constructor-arg ref="servicioItems"/>
</bean>
<!-- Declaramos el endpoint de nuestro servicio web, indicando cual es la
clase de implementación. En el atributo "implementor" podemos escribir
el nombre completo de la clase implementación, o referenciar a un
bean de Spring usando un # seguido del nombre del bean.
-->
<jaxws:endpoint
id="holaMundo"
implementor="#holaMundoImpl"
address="/HolaMundo" />
<jaxws:endpoint
id="store"
implementor="#storeImpl"
address="/StoreImpl" />
<jaxws:endpoint
id="storeAdmin"
implementor="#storeAdminImpl"
address="/StoreAdminImpl" />
这是我的测试junit文件:
@Test
public void testGetItem()
{
Logger.getLogger(StoreAdminTest.class.getName()).log(Level.INFO, "INIT testGetDescriptionItem");
try
{
boolean retorno = instance.addItemToSystem("8717418323691", "Castle Tercera Temporarada DVD",39.95,100);
assertEquals(true,retorno);
//broken here
//org.apache.cxf.interceptor.Fault: Marshalling Error: com.dosideas.cxf.Item is not known to this context
Item item = instance.getItem("8717418323691");
assertEquals(true, item!=null);
assertEquals("Castle Tercera Temporarada DVD",item.getDescripcion());
} catch (StoreException ex) {
Logger.getLogger(StoreAdminTest.class.getName()).log(Level.SEVERE, null, ex);
}
Logger.getLogger(StoreAdminTest.class.getName()).log(Level.INFO, "END testGetDescriptionItem");
}
项目 pojo 类:
package com.dosideas.cxf;
/**
*
* @author alonso
*/
public class Item {
private String isbn;
private String descripcion;
private double precio;
private int numeroTotal;
Item(){};
Item(String isbn,String descripcion,double precio,int numeroTotal)
{
this.isbn=isbn;
this.descripcion=descripcion;
this.precio=precio;
this.numeroTotal=numeroTotal;
};
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getNumeroTotal() {
return numeroTotal;
}
public void setNumeroTotal(int numeroTotal) {
this.numeroTotal = numeroTotal;
}
public double getPrecio() {
return precio;
}
public void setPrecio(double precio) {
this.precio = precio;
}
public void decrementarNumeroTotal()
{
if (getNumeroTotal()>0)
setNumeroTotal(getNumeroTotal() - 1);
else
setNumeroTotal(0);
}
}
异常日志:
org.apache.cxf.interceptor.Fault: Marshalling Error: com.dosideas.cxf.Item is not known to this context
我不知道怎么回事,我花了 3 天时间阅读博客,请帮助!!
【问题讨论】:
-
您的
Item类没有被 JAXB 注释。看起来你是这项技术的新手,如果是这样,我建议你使用wsimport从 WSDL 生成 bean,试试这个,如果你有信心,修改 bean。 -
感谢 dma_k 的响应,我是 cxf 的新手,但不开发 web 服务。看起来 cxf 没有编组项目 pojo,为什么该死的地狱不这样做?同时,我会检查 wsimport
-
你好,我已经解决了我的问题,抱歉,默认范围构造函数没有公开,arggg。无论如何,谢谢答案
标签: cxf