嗨,我是cxf soap Web服务的新手,我在测试此api时创建了一个crud应用程序,但在SOAPUI中收到以下错误消息
<faultstring>object is not an instance of declaring class while invoking public abstract soap.product.pojo.Product soap.product.service.ProductService.getProductById(int) with params [1]</faultstring>
Product.java
package soap.product.pojo;
public class Product
{
private int id;
private String name;
private String description;
private float price;
public Product(int id, String name, String description, float price)
{
super();
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
public Product()
{
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price = price;
}
}
ProuductDao.java
package soap.product.dao;
import java.util.List;
import soap.product.pojo.Product;
public interface ProductDao
{
public int saveProduct(Product product);
public int updateProduct(Product product);
public Product findProductById(int id);
public int deleteProductById(int id);
public List<Product> getAllProductsList();
}
ProductDaoImpl.java
package soap.product.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import soap.product.pojo.Product;
public class ProductDaoImpl implements ProductDao
{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}
public int saveProduct(Product product)
{
int savedProduct = jdbcTemplate.update("INSERT INTO PRODUCT VALUES("+product.getId()+",'"+product.getName()+"','"+product.getDescription()+"',"+product.getPrice()+")");
return savedProduct;
}
public int updateProduct(Product product)
{
int updatedProduct = jdbcTemplate.update("UPDATE PRODUCT SET NAME = '"+product.getName()+"', DESCRIPTION = '"+product.getDescription()+"', PRICE = "+product.getPrice()+" WHERE ID = "+product.getId()+"");
return updatedProduct;
}
public Product findProductById(int id)
{
return jdbcTemplate.query("SELECT * FROM PRODUCT WHERE ID ="+id, new ResultSetExtractor<Product>()
{
public Product extractData(ResultSet rs) throws SQLException, DataAccessException
{
Product product=new Product();
while(rs.next())
{
product.setId(rs.getInt("ID"));
product.setName(rs.getString("NAME"));
product.setDescription(rs.getString("DESCRIPTION"));
product.setPrice(rs.getFloat("PRICE"));
}
return product;
}
});
}
public int deleteProductById(int id)
{
return jdbcTemplate.update("DELETE FROM PRODUCT WHERE ID = "+ id);
}
public List<Product> getAllProductsList()
{
return jdbcTemplate.query("SELECT * FROM PRODUCT", new ResultSetExtractor<List<Product>>()
{
public List<Product> extractData(ResultSet rs) throws SQLException, DataAccessException
{
List<Product> allProducts = new ArrayList<Product>();
while(rs.next())
{
Product product = new Product();
product.setId(rs.getInt("ID"));
product.setName(rs.getString("NAME"));
product.setDescription(rs.getString("DESCRIPTION"));
product.setPrice(rs.getFloat("PRICE"));
allProducts.add(product);
}
return allProducts;
}
});
}
}
ProductService.java
package soap.product.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import soap.product.pojo.Product;
@WebService(name="ProductService")
@SOAPBinding(style=Style.DOCUMENT, use=Use.LITERAL)
public interface ProductService
{
@WebMethod
@WebResult(name="Product") Product saveProduct(@WebParam(name="product")Product product);
@WebMethod
@WebResult(name="Product") Product updateProduct(@WebParam(name="product")Product product);
/*@WebMethod
public @WebResult List<Product> getAllProdcuts()throws ProductServiceException;*/
@WebMethod
@WebResult(name="Product") Product getProductById(@WebParam(name="id")int id);
@WebMethod
void deleteProductById(@WebParam(name="id")int id);
}
ProductServiceImpl.java
package soap.product.service;
import soap.product.dao.ProductDao;
import soap.product.pojo.Product;
public class ProductServiceImpl implements ProductService
{
ProductDao productDao;
public void setProductDao(ProductDao productDao)
{
this.productDao = productDao;
}
@Override
public Product saveProduct(Product product)
{
int saveProduct = productDao.saveProduct(product);
System.out.println("saveProduct "+saveProduct);
return product;
}
@Override
public Product updateProduct(Product product)
{
int updateProduct = productDao.updateProduct(product);
System.out.println("updateProduct"+updateProduct);
return product;
}
/*@Override
public List<Product> getAllProdcuts() throws ProductServiceException
{
List<Product> allProductsList = productDao.getAllProductsList();
return allProductsList;
}*/
@Override
public Product getProductById(int id)
{
Product foundProduct = productDao.findProductById(id);
return foundProduct;
}
@Override
public void deleteProductById(int id)
{
int deleteProductById = productDao.deleteProductById(id);
System.out.println("Product Deleted"+deleteProductById);
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-and-cxf-configuration.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
spring-and-cxf-configuration.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:beans="http://cxf.apache.org/configuration/beans"
xmlns:core="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core" xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd">
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/soapdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds" />
</bean>
<bean id="dao" class="soap.product.dao.ProductDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="productService" class="soap.product.service.ProductServiceImpl">
<property name="productDao" ref="dao" />
</bean>
<jaxws:server id="productService" serviceClass="soap.product.service.ProductService"
address="/productSoapService">
<jaxws:serviceBean>
<ref bean="productService"></ref>
</jaxws:serviceBean>
</jaxws:server>
<!-- <jaxws:server id="productService" address="/productService"> <jaxws:serviceBean>
<ref bean="productService" /> </jaxws:serviceBean> </jaxws:server> -->
<cxf:bus>
<cxf:features>
<cxf:logging />
</cxf:features>
</cxf:bus>
</beans>
有人可以帮助我解决问题吗?