I created one webservice using apache cxf
maven and deployes in
tomcat. But when I write client program if the methods has parameters in it . Those parameters have been again initialized to default values in webservice implementation . here is the code snippet kindly suggest any changes required.
studentService.java
package com.web.service;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.web.beans.Student;
import com.web.output.studentServiceResponse;
@WebService
public interface studentService {
@WebMethod
public studentServiceResponse insertStudent(Student s);
@WebMethod
public studentServiceResponse updateStudent(Student s);
@WebMethod
public studentServiceResponse deleteStudent(Student s);
@WebMethod
public void rtriveStudent();
@WebMethod
public Date getDate();
}
studentServiceImpl.java
package com.web.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.log4j.Logger;
import com.web.beans.Student;
import com.web.db.getConnection;
import com.web.output.studentServiceResponse;
@WebService(endpointInterface="com.web.service.studentService")
public class studentServiceImpl implements studentService{
@Override
@WebMethod(operationName = "inserStudent")
public studentServiceResponse insertStudent(@WebParam(name="student") Student s) {
Connection con=null;
int rs =0;
System.out.println("In Insert student .......");
System.out.println(s);
studentServiceResponse response = new studentServiceResponse();
try{
getConnection db=new getConnection();
con=db.getDBConnection();
String Query = "INSERT INTO studentinfo VALUES("+s.getId()+",'"+s.getName()+"',"+s.getAge()+")";
PreparedStatement ps = con.prepareStatement(Query);
rs=ps.executeUpdate();
if(rs>0){
response.setMessage("Student Values are updated successfully");
}
else{
response.setMessage("Student Values are not updated successfully");
}
return response;
}
catch(Exception e){
response.setMessage("Student Values are not updated successfully");
}
finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return response;
}
@Override
public studentServiceResponse updateStudent(Student s) {
Connection con=null;
int rs =0;
studentServiceResponse response = new studentServiceResponse();
try{
getConnection db=new getConnection();
con=db.getDBConnection();
String Query = "UPDATE studentinfo SET name='"+s.getName()+"',age="+s.getAge()+" WHERE id="+s.getId();
PreparedStatement ps = con.prepareStatement(Query);
rs = ps.executeUpdate();
if(rs>0){
response.setMessage("Student Values are updated successfully");
}
else{
response.setMessage("Student Values are not updated successfully");
}
return response;
}
catch(Exception e){
response.setMessage("Student Values are not updated successfully");
}
finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return response;
}
@Override
public studentServiceResponse deleteStudent(Student s) {
Connection con=null;
int rs =0;
studentServiceResponse response = new studentServiceResponse();
try{
getConnection db=new getConnection();
con=db.getDBConnection();
String Query ="DELETE FROM studentinfo WHERE id="+s.getId();
System.out.println(Query);
PreparedStatement ps = con.prepareStatement(Query);
rs = ps.executeUpdate();
System.out.println("The value of Rs is :"+rs);
if(rs>0){
response.setMessage("Student Values are updated successfully");
}
else{
response.setMessage("Student Values are not updated successfully");
}
return response;
}
catch(Exception e){
response.setMessage("Student Values are not updated successfully");
}
finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return response;
}
public Date getDate(){
return new Date();
}
@Override
public void rtriveStudent() {
Connection con = null;
try{
getConnection db=new getConnection();
con=db.getDBConnection();
String Query = " SELECT count(*) FROM studentinfo";
PreparedStatement ps = con.prepareStatement(Query);
int count =0;
ResultSet rs = ps.executeQuery();
while(rs.next()){
count++;
}
System.out.println("The total numbe rof records :"+count);
}
catch(Exception e){
System.out.println(e);
}
}}
appContext.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:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.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"
default-autowire="byName">
<!-- Load CXF modules from cxf.jar -->
<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" />
<!-- Enable message logging using the CXF logging feature -->
<cxf:bus>
<cxf:features>
<cxf:logging />
</cxf:features>
</cxf:bus>
<!-- The service bean -->
<bean id="studentServiceImpl"
class="com.web.service.studentServiceImpl" />
<!-- Aegis data binding -->
<bean id="aegisBean"
class="org.apache.cxf.aegis.databinding.AegisDatabinding"
scope="prototype" />
<bean id="jaxws-and-aegis-service-factory"
class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"
scope="prototype">
<property name="dataBinding" ref="aegisBean" />
<property name="serviceConfigurations">
<list>
<bean
class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration" />
<bean
class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration" />
<bean
class="org.apache.cxf.service.factory.DefaultServiceConfiguration" />
</list>
</property>
</bean>
<!-- Service endpoint -->
<jaxws:endpoint id="studentService"
implementorClass="com.web.service.studentServiceImpl"
implementor="#studentServiceImpl" address="/studentService">
<jaxws:serviceFactory>
<ref bean="jaxws-and-aegis-service-factory" />
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="services" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appContext.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>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Log4JTestServlet</servlet-name>
<servlet-class>test.Log4JTestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Log4JInitServlet</servlet-name>
<servlet-class>test.Log4JInitServlet</servlet-class>
<init-param>
<param-name>log4j-properties-location</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Log4JTestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
I created web client using a configuration file and created client program
client.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.web.service.studentService"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.web.service.studentService"/>
<property name="address" value="http://localhost:8080/studentService/services/studentService"/>
</bean>
</beans>
<!-- END SNIPPET: beans -->
ClientMain.java
package com.web.client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import com.web.beans.Student;
import com.web.service.studentService;
public class clientMain {
public static void main(String[] args) {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
BeanFactory context = new XmlBeanFactory(new FileSystemResource("src/main/java/com/web/client/client-beans.xml"));
studentService service = (studentService)context.getBean("client");
System.out.println(service.getDate());
Student student = new Student();
student.setId(2);
student.setName("Eresh");
student.setAge(25);
System.out.println(service.insertStudent(student));
//student.setId(1);
//System.out.println(service.deleteStudent(student));
//service.rtriveStudent();
}
}
In the above client program I am able to retreive date from getDate() but I am unable to process the request from insertStudent(Student s)
The out put is :
Dec, 2012 10:58:49 PM org.apache.cxf.bus.spring.BusApplicationContext getConfigResources
INFO: No cxf.xml configuration file detected, relying on defaults.
3 Dec, 2012 10:58:50 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://service.web.com/}studentServiceService from class com.web.service.studentService
Mon Dec 03 22:58:50 IST 2012
studentServiceResponse [message=null]
I cannot figure out why insertStudent is unable to insert in database in service Impl class the values of student object again set to [0,null,0]
Can any one help me in correcting this , tell me where i am mistaken.
Thanks
Eresh