欢迎访问小程序员!

基于Rribbit和Spring MVC搭建REST风格架构

2014-09-22 0条评论 720次浏览

基于Rribbit事件驱动和Spring MVC搭建Restful风格的架构步骤 :

 

配置Pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wx</groupId>
	<artifactId>sipbus</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>sipbus Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<build>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.19</version>
				<configuration>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<contextPath>sipbus</contextPath>
					<connectors>
						<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
							<port>7000</port>
							<maxIdleTime>60000</maxIdleTime>
						</connector>
					</connectors>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<webappDirectory>${project.build.directory}/bin/sipbus.war
					</webappDirectory>
					<useCache>false</useCache>
					<dependentWarExcludes>WEB-INF/lib/*-1.0-SNAPSHOT*
					</dependentWarExcludes>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${basedir}/src/main/webapp/WEB-INF/lib
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.rribbit</groupId>
			<artifactId>rribbit</artifactId>
			<version>2.7.0</version>
			<type>jar</type>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.1.2.RELEASE</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.1.2.RELEASE</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.1.2.RELEASE</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>

 

Rribbit配置:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="creator"
		class="org.rribbit.creation.SpringBeanClassBasedListenerObjectCreator">
		<property name="packageNames">
			<list>
				<value>com.geone.its</value>
			</list>
		</property>
		<property name="scanSubpackages" value="true" />
	</bean>
	<bean id="rrb" class="org.rribbit.util.RRiBbitUtil"
		factory-method="createRequestResponseBusForLocalUse">
		<constructor-arg ref="creator" />
		<constructor-arg value="true" />
	</bean>
</beans>

 

Spring MVC配置:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="classpath*:rribbit-context.xml" />
	<context:annotation-config />
	<context:component-scan base-package="com.wx" />
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

 

Rribbit测试类:

@Service("testRribbit")
public class TestRribbit {
	@Listener(hint = "testEvent")
	public void testEvent() {
		System.out.println("******rribbit********");
	}
}

 

Rest测试类:

@Controller  
@RequestMapping("/test")
public class TestRest {
	@Autowired
	private RequestResponseBus rrb;
	@RequestMapping("/hello.do")  
    public void printWelcome(ModelMap model) {  
        model.addAttribute("message", "testing"); 
        System.out.println("*************testing**********");
        rrb.send("testEvent");
    }  
}

 

执行mvn package

mvn jetty:run

 

visit url:http://localhost:7000/testRest/test/hello

 

信息: FrameworkServlet 'springMvc': initialization completed in 462 ms

*************testing**********

******rribbit********

发表评论

登录,将以游客形式发表

网友最新评论(0)