ssh学习笔记二:cxfServlet简单实现
Posted RunningFan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh学习笔记二:cxfServlet简单实现相关的知识,希望对你有一定的参考价值。
1 spring配置学习
1.1 配置bean
学习一个例子。测试配置bean,配置两个family,family1和family2都有相同的属性但是属性值不同,加入配置文件,可以配置如下:
1.1.1 配置bean的属性值spring-base.xml
<!-- 使用class属性指定类的默认构造方法创建实例 名称由id属性指定 加上scope="prototype" 表示每次生成新的实例-->
<bean id="family" class="com.study.spring.Family" >
<property name="mother" value="Lily"></property>
<property name="father" value="Bob"></property>
<property name="sister" value="Jerry"></property>
<property name="brother" value="Ham"></property>
</bean>
<bean id="familyUtil1" class="com.study.spring.FamilyUtil">
<property name="family" ref="family"></property>
</bean>
<bean id="family2" class="com.study.spring.Family" >
<property name="mother" value="family2-Lily"></property>
<property name="father" value="family2-Bob"></property>
<property name="sister" value="family2-Jerry"></property>
<property name="brother" value="family2-Ham"></property>
</bean>
<bean id="familyUtil2" class="com.study.spring.FamilyUtil">
<property name="family" ref="family2"></property>
</bean>
1.1.2 Family是个实体类
package com.study.spring;
public class Family
private String mother;
private String father;
private String sister;
private String brother;
public String getMother()
return mother;
public void setMother(String mother)
this.mother = mother;
public String getFather()
return father;
public void setFather(String father)
this.father = father;
public String getSister()
return sister;
public void setSister(String sister)
this.sister = sister;
public String getBrother()
return brother;
public void setBrother(String brother)
this.brother = brother;
1.1.3 FamilyUtil类,包含family属性
package com.study.spring;
public class FamilyUtil
private Family family = new Family();
public Family getFamily()
return family;
public void setFamily(Family family)
this.family = family;
public String listFamily()
if(family.getMother() != null)
System.out.println(family.getMother());
System.out.println(family.getFather());
System.out.println(family.getBrother());
System.out.println(family.getSister());
return "success";
else
System.out.println("为空");
return "fail";
1.1.4 测试类TestFamily
package com.study.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.study.utils.struts.BaseAction;
@Controller("testFamily")
@Scope("prototype")
public class TestFamily extends BaseAction
/**
* @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
*/
private static final long serialVersionUID = 1L;
@Autowired
@Qualifier("familyUtil1")
private FamilyUtil familyUtil1;
@Autowired
@Qualifier("familyUtil2")
private FamilyUtil familyUtil2;
public void listFamily()
if("success".equals(familyUtil1.listFamily()))
writeJson2Page("success");
else
writeJson2Page("fail");
public void listFamily2()
if("success".equals(familyUtil2.listFamily()))
writeJson2Page("success");
else
writeJson2Page("fail");
1.1.5 调用方法
调用TestFamily.listFamily()时,控制台打印结果
Lily
Bob
Ham
Jerry
调用TestFamily.ListFamily2()时,控制台打印结果
family2-Lily
family2-Bob
family2-Ham
family2-Jerry
结果与预期的一样。
1.1.6 结果分析
TestFamily通过注解的方式加载bean “familyUtil1”,在spring-base.xml配置文件中
即,familyUtil1有family属性,且指向bean “family”,而bean “family”又有以下属性
<property name="mother" value="Lily"></property>
<property name="father" value="Bob"></property>
<property name="sister" value="Jerry"></property>
<property name="brother" value="Ham"></property>
故调用familyUtil1.listFamily()时会显示配置文件的family属性配置。调用familyUtil2.listFamily同理会显示family2的属性配置。
2 简单的cxfServlet实例
2.1 什么是cxfServlet
CXF implements the JAX-WS APIs which make building web services easy.
即是apache的一个webservice引擎。
2.2 依赖的jar包
spring4依赖的包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
cxf3依赖的jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.0-milestone1</version>
</dependency>
2.3 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>gyh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*.xml</param-value>
</context-param>
<!-- CXF implements the JAX-WS APIs which make building web services easy.apache的一个webservice引擎。 -->
<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>/service/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.4 spring-cxf.xml加入配置
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<bean id="helloService" class="com.study.spring.HelloServiceImpl"></bean>
<jaxrs:server id="defaultService" address="/">
<jaxrs:serviceBeans>
<ref bean="helloService"/>
</jaxrs:serviceBeans>
</jaxrs:server>
2.5 代码如下
接口类IHelloService
package com.study.spring;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.study.utils.spring.IBaseAction;
@Path("/hello")
public interface IHelloService extends IBaseAction
@GET
@Path("/sayHello")
@Produces(MediaType.TEXT_HTML,MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON)
public String sayHello(@FormParam("msg")String msg);
接口实现类HelloServiceImpl
package com.study.spring;
public class HelloServiceImpl implements IHelloService
@Override
public String sayHello(String msg)
System.out.println("Hello World!" + msg);
return "success";
2.6 访问接口
启动tomcat,访问地址由tomcat的地址+项目名称(tomcat下的server.xml可以修改)+web.xml中cxfServlet配置的 地址+spring-cxf.xml中配置的address+interface配置path
如我的ip地址 192.168.100.115:8081,项目名gyh,
web.xml配置 “/service”,
spring-cxf.xml配置 “/”
类Path: “/hello”
方法Path: “/sayHello”
则访问地址 :http://192.168.100.115:8081/gyh/service/hello/sayHello
加参数访问:http://192.168.100.115:8081/gyh/service/hello/sayHello?msg=Sweet
浏览器结果
控制台输出,上面为加参数访问结果
以上是关于ssh学习笔记二:cxfServlet简单实现的主要内容,如果未能解决你的问题,请参考以下文章