首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

CXF+SPRING+REST 十分钟教程

2012-09-02 
CXF+SPRING+REST 10分钟教程CXF跟spring联合起来,搞rest webservice的确很方便的.下面快速学习下,用到的是

CXF+SPRING+REST 10分钟教程

CXF跟spring联合起来,搞rest webservice的确很方便的.下面快速学习下,用到的是CXF,SPRING和MAVEN?

1 MAVEN配置?
<dependency>?
??? <groupId>org.springframework</groupId>?
??? <artifactId>spring-context</artifactId>?
??? <version>3.0.1.RELEASE</version>?
</dependency>?
<dependency>?
??? <groupId>org.springframework</groupId>?
??? <artifactId>spring-web</artifactId>?
??? <version>3.0.1.RELEASE</version>?
</dependency>?
<dependency>?
??? <groupId>org.apache.cxf</groupId>?
??? <artifactId>cxf-rt-frontend-jaxrs</artifactId>?
??? <version>2.2.3</version>?
</dependency>?
2 COMPILER也改下?
<plugin>?
??? <groupId>org.apache.maven.plugins</groupId>?
??? <artifactId>maven-compiler-plugin</artifactId>?
??? <version>2.1</version>?
??? <configuration>?
??????? <source>1.6</source>?
??????? <target>1.6</target>?
??? </configuration>?
</plugin>?
...?

3 timeservice?
? import org.springframework.format.datetime.DateFormatter;?
import org.springframework.stereotype.Service;?

import java.util.Calendar;?
import java.util.Locale;?

@Service("timeService")?
public class TimeService {?
??? public String getDateTime()?
??? {?
??????? DateFormatter formatter = new DateFormatter("dd/MM/yyyy hh:mm:ss");?
??????? return formatter.print(Calendar.getInstance().getTime(), Locale.getDefault());?
??? }?
}?

4 CXF使用JAX-RS 去实现REST?
? @Service("timeService")?
@Path("/time")?
public class TimeService {?
??? @GET?
??? @Produces("text/plain")?
??? public String getDateTime()?
??? {?
?????? ...?
??? }?
}?

5 配置WEB.XML?
? <?xml version="1.0" encoding="UTF-8"?>?
<!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>?
??? <context-param>?
??????? <param-name>contextConfigLocation</param-name>?
??????? <param-value>WEB-INF/beans.xml</param-value>?
??? </context-param>?
??? <listener>?
??????? <listener-class>?
??????????? org.springframework.web.context.ContextLoaderListener?
??????? </listener-class>?
??? </listener>?
??? <servlet>?
??????? <servlet-name>CXFServlet</servlet-name>?
??????? <display-name>CXF Servlet</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>/rest/*</url-pattern>?
??? </servlet-mapping>?
</web-app>?


6 配置CXF配置文件?
?? <?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:jaxrs="http://cxf.apache.org/jaxrs"?
?????? xmlns:context="http://www.springframework.org/schema/context"?
?????? xsi:schemaLocation="?
?????? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd?
?????? http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd?
?????? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">?

??? <import resource="classpath:META-INF/cxf/cxf.xml"/>?
??? <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>?
??? <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>?

??? <context:component-scan base-package="be.insaneprogramming.cxf"/>?

??? <jaxrs:server id="restContainer" address="/">?
??????? <jaxrs:serviceBeans>?
??????????? <ref bean="timeService"/>?
??????? </jaxrs:serviceBeans>?
??? </jaxrs:server>?
</beans>?

1 楼 zjc198805 2012-04-27   lz,如果遇到注册的时候又要上传图片又要上传注册的信息该怎么搞?求指教!644952013@qq.com !求客户端与服务器端,客户端只要写一个main方法就好! 2 楼 ibadboy 2012-05-29   zjc198805 写道lz,如果遇到注册的时候又要上传图片又要上传注册的信息该怎么搞?求指教!644952013@qq.com !求客户端与服务器端,客户端只要写一个main方法就好!

这个要看你的需求,你可以这样做:
1、利用ajax上传控件(带flash的,开源的很多)把图片从本地读出来预览,等注册信息都填写好后一起提交到后台.
2、先用ajax上传图片资源,并且返回生成的图片名称到前台,填写注册信息后再提交到后台,如果注册失败你也可以删除刚才上传的图片。


总之,方法很多,多思考,希望能帮到你。

热点排行