首页 > 其他分享 >10分钟使用CXF和SPRING创建REST webservice应用

10分钟使用CXF和SPRING创建REST webservice应用

时间:2022-12-02 13:01:32浏览次数:42  
标签:10 www http webservice SPRING CXF springframework context org


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>

标签:10,www,http,webservice,SPRING,CXF,springframework,context,org
From: https://blog.51cto.com/u_14230175/5906949

相关文章

  • 使用CXF创建REST WEBSERVICE
    简单小结下CXF跟REST搭配webservice的做法,直接举代码为例子:1order.javapackagecom.example.rest;importjavax.xml.bind.annotation.XmlRootE......
  • spring接口重放过滤问题
    1、定义注释importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.anno......
  • Spring Cloud OpenFeign
    该项目通过自动配置为SpringBoot应用程序提供OpenFeign集成并绑定到Spring环境和其他Spring编程模型习语。1.声明式REST客户端:假装Feign是一个声明式Web服务......
  • 复盘10步法
       ......
  • vs.net 2010两个数据库方面的好工具
    今天发现vs.net2010在处理数据库方面的两个不错的工具,分别是数据异同比较器和数据架构比较器,使用起来都很简单,要学习的话,可以从下面两个连接......
  • 代码讲解spring中的singleton和prototype
    1.singleton配置中的bean定义可以看作是一个模板,容器会根据这个模板来构造对象。bean定义中的scope语义会决定:容器将根据这个模板构造多少对象实例,......
  • CXF spring jaxws:endpoint jaxws:server 区别 与 关系
    First,theyareallfortheserversideconfiguration.Second,jaxws:endpointiscomingfromJAXWSAPI,anditisusedtoconfiguretheo......
  • springIOC和AOP
    IOC(InverseofControl:控制反转)是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来管理。Ioc在其他语言中也有应用,并非spring特有。Ioc容器是Spring用......
  • shiro低版本更新到高版本(>1.10.0)出现报错问题解决
    近期漏洞爆出(ApacheShiro<1.10.0身份认证绕过漏洞),开始升级新版的jar包。升级过程1.修改pom文件shiro版本<!--shiro--><dependency><groupId>org.apache.......
  • spring mvc中普通类获得request
    springmvc中,在控制层很容易获得request,response,但在其他层的话获得的话,其实必要性不大,但可以实现:在web.xml中加入<listener><list......