码迷,mamicode.com
首页 > 编程语言 > 详细

cxf整合spring代码

时间:2018-04-10 23:58:12      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:ica   splay   接口   导入jar包   findall   sdl   地址   index.jsp   xfs   

导入jar包cxf的jar包

创建实体类

package com.yhd.webservice.cxf.server.poto;

public class Person {


private String name;
private int age;

public Person() {
}

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

创建接口

package com.yhd.webservice.cxf.server.service;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.yhd.webservice.cxf.server.poto.Person;

@WebService
public interface PersonService {

public List<Person> findAll(@WebParam(name = "arg0") String name);

}

创建实现类

package com.yhd.webservice.cxf.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.yhd.webservice.cxf.server.poto.Person;
import com.yhd.webservice.cxf.server.service.PersonService;

@WebService(endpointInterface="com.yhd.webservice.cxf.server.service.PersonService",serviceName="person")
public class PersonServiceImp implements PersonService {

@Override
public List<Person> findAll(String name){
ArrayList<Person> persons = new ArrayList<Person>();

Person p1 = new Person();
p1.setName(name + "3");
p1.setAge(18);

Person p2 = new Person();
p2.setName(name + "4");
p2.setAge(20);

persons.add(p1);
persons.add(p2);

return persons;
}
}

创建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
xmlns="http://www.springframework.org/schema/beans">

<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
</bean>

<jaxws:endpoint id="serviceimp" address="/person"
implementor="com.yhd.webservice.cxf.server.service.impl.PersonServiceImp">

<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebServiceServer</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<display-name>cxfTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-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>/hello/*</url-pattern>
</servlet-mapping>

</web-app>

 

测试服务有没有发布成功  http://localhost:8080/cxf_spring/hello   服务地址

 

 

wsdl2java -d .      ?wsdl生成客户端调用代码

测试类

package com.yhd.webservice.cxf.client.main;

import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import per.Person;
import per.PersonService;

public class ClientTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(PersonService.class);
factory.setAddress("http://localhost:8080/cxf_spring/hello/person");
PersonService service = (PersonService)factory.create();
List<Person> list = (List<Person>)service.findAll("张");
for (Person person : list) {
System.out.println("name=" + person.getName());
System.out.println("age=" + person.getAge());
System.out.println("-------------------");
}
}

}

cxf整合spring代码

标签:ica   splay   接口   导入jar包   findall   sdl   地址   index.jsp   xfs   

原文地址:https://www.cnblogs.com/fdbk/p/8783456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!