CXF Webservice两种方式--soap+restful

soap方式
1.java部分
import javax.jws.WebService;  

@WebService  
public interface HelloService {  
      
    public String sayHello(String name);  
      
} 

import javax.jws.WebService;

@WebService(endpointInterface = "com.ustc.service.HelloService",serviceName="helloService")  
public class HelloServiceImpl implements HelloService{  
  
    public String sayHello(String name) {  
        
        return "hello"+name;  
    }  
  
} 
2.xml部分
applicationContext-client.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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
                       
	<!-- 此时cxf版本为3.0 -->
    <!-- 方法一 -->
	<jaxws:client id="helloServiceClient"  
                  serviceClass="com.ustc.service.HelloService"  
                  address="http://localhost:8080/ws/helloService" />  
    <!-- 方法二 -->         
    <!--  <bean id="helloServiceClient" class="com.ustc.service.HelloService" factory-bean="clientFactory" factory-method="create" />
                   
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.ustc.service.HelloService" />
        <property name="address" value="http://localhost:8080/ws/helloService" />
    </bean> -->
</beans>
applicationContext-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                       http://www.springframework.org/schema/context   
    					http://www.springframework.org/schema/context/spring-context-3.0.xsd   
                       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                       http://cxf.apache.org/jaxrs  
                       http://cxf.apache.org/schemas/jaxrs.xsd
                       http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
						
	    <!-- 启动触发器的配置开始 -->  
    <bean name="startQuertz" lazy-init="false" autowire="no"  
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <ref bean="myJobTrigger" />  
            </list>  
        </property>  
    </bean>  
    <!-- 启动触发器的配置结束 -->  
  
    <!-- 调度的配置开始 -->  
    <!--  
        quartz-1.8以前的配置   
    <bean id="myJobTrigger"  
        class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="jobDetail">  
            <ref bean="myJobDetail" />  
        </property>  
        <property name="cronExpression">  
            <value>0/1 * * * * ?</value>  
        </property>  
    </bean>  
    -->  
    <!-- quartz-2.x的配置 -->  
    <bean id="myJobTrigger"  
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail">  
            <ref bean="myJobDetail" />  
        </property>  
        <property name="cronExpression">  
            <value>0/5 * * * * ?</value>  
        </property>  
    </bean>  
    <!-- 调度的配置结束 -->  
  
    <!-- job的配置开始 -->  
    <bean id="myJobDetail"  
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="myJob" />  
        </property>  
        <property name="targetMethod">  
            <value>work</value>  
        </property>  
    </bean>  
    <!-- job的配置结束 -->  
  
    <!-- 工作的bean -->  
    <bean id="myJob" class="com.ustc.test.MyJob" /> 
	<!-- 开启mvc注解 -->
	<mvc:annotation-driven />
                       
   <!-- 注解扫描包 -->
	<context:component-scan base-package="com.ustc" />
     
    
 	<bean id="personService" class="com.ustc.service.PersonServiceImpl" />
 	

	
    <!-- soap webservice -->  
    <jaxws:endpoint id="helloService" implementor="com.ustc.service.HelloServiceImpl" address="/helloService" />
    <!-- restful webservice -->  
    <jaxrs:server id="rs2" address="/rs2">  
        <jaxrs:serviceBeans>  
            <ref bean="personService" />  
        </jaxrs:serviceBeans>  
        <jaxrs:providers>  
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />  
        </jaxrs:providers>  
    </jaxrs:server>     
</beans>
restful方式
1.java部分
@Produces( { MediaType.APPLICATION_JSON }) 
public interface PersonService extends Serializable {
    @GET
    @Path("/persons")
    public List<Person> getPersons();

    @GET
    @Path("/persons/{id}")
    public Person getPerson(@PathParam("id") String id);
    
    @GET  
    @Path(value = "/search")  
    public Person findPersonById(@QueryParam("id")String id);  
} 
public class PersonServiceImpl implements PersonService {
    private static final long serialVersionUID = 1L;
    private static Map<String, Person> ps = new HashMap<String, Person>();
    static {
        Person p1 = new Person();
        p1.setId("1");
        p1.setUsername("zhangsan");
        p1.setDescription("hello");

        Person p2 = new Person();
        p2.setId("2");
        p2.setUsername("lisi");
        p2.setDescription("lisi hehe");

        ps.put(p1.getId(), p1);
        ps.put(p2.getId(), p2);
    }

    public Person getPerson(String id) {
        return ps.get(id);
    }

    public List<Person> getPersons() {
        return new ArrayList(ps.values());
    }

	public Person findPersonById(String id) {
		// TODO Auto-generated method stub
		return ps.get(id);
	}
}
@XmlRootElement(name = "person")
public class Person implements Serializable {
    private String id;
    private String username;
    private String description;
    @XmlElement(value = "id")
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    

	@XmlElement(value = "name")
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
    @XmlElement(value = "description")
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
    
}
2.xml部分
同上

附:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>CXFDemo</display-name>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext-server.xml,classpath*:applicationContext-client.xml</param-value>
  </context-param>
 
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置SpringMVC核心控制器 -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 -->  
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
		</init-param>
		<!-- 启动加载一次 -->  
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!--为DispatcherServlet建立映射 -->
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <display-name>CXFServlet</display-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>/ws/*</url-pattern>
  </servlet-mapping>
  
   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springmvc的restful风格
@Controller
@RequestMapping(value = "/api/person")
public class PersonController {
	//注入webservice
	@Resource
	private HelloService helloServiceClient;
	
	PersonServiceImpl service=new PersonServiceImpl();
	/* 
	 * 1.@PathVariable
	 * 2.@RequestParam
	 * */
	
	@ResponseBody
	@RequestMapping(value="/index",method = RequestMethod.GET,produces="application/json")
	public Person show(@RequestParam String id ) {
		Person p=service.getPerson(id);
        System.out.println(id);
		return p;
	}
	
	@ResponseBody
	@RequestMapping(value="/test",method = RequestMethod.GET,produces="application/json")
	public String test() {
		
		return helloServiceClient.sayHello("china");
	}
}

public class Client {
	/* Autowired byType Service byName*/
	
	 //此时可不启动web项目,直接测试
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        //HelloWorld helloService = (HelloWorld) context.getBean("helloService");
        HelloService helloService = (HelloService) context.getBean(HelloService.class);
        String response = helloService.sayHello("Peter");
        System.out.println(response);
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值