您的位置:首页 > 编程语言 > Java开发

利用resteasy框架构建rest webservice----第四波:resteasy与spring真正集成发布我们的restful webservice 服务(实例、教程)

2014-08-03 09:46 911 查看
思路:这节我们把resteasy和spring结合了(非spring mvc),我的理解就是资源都交给spring来管理了,我们需要的资源需要到spring容器之中去获取



问题汇总:一直出现org.jboss.resteasy.plugins.spring.SpringContextLoaderListener这个类找不到,而实际上本地仓库是已经有了,经过检查eclipse映射的发布项目

发现没有resteasy-spring这个JAR包拿过去

1. <context-param>

<param-name>resteasy.scan.resources</param-name>

<param-value>true</param-value>

</context-param> -->

这个东西可能不能写,写了发现有问题,原因不详

2.如果使用m2eclipse的话,可能必须在m2eclipse中执行eclipse命令

3.不得已的话添加MAVEN依赖试试



原本以为根据文档集成resteasy与spring很简单,但是却还是花费的一定时间,避免大家走弯路----现在网上有的相关的集成都只是,spring加入进项目,木有与resteasy真正集成

老规矩,先上MAVEN的配置文件中依赖包:

[html]
view plaincopy

<dependencies>
<!-- core library -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>

<!-- optional modules -->
<!-- Spring integration -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>2.2.1.GA</version>
</dependency>

<!-- JAXB support -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.2.1.GA</version>
</dependency>

<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
</dependencies>

注意上图中得resteasy-spring模块依赖,和spring依赖

resteasy-spring直接配置似乎不能自动下载,如果不行可以自己去官网下载然后安装到私服仓库

还有spring的依赖必须是要有的,resteasy-spring只是提供集成代码

2.关键的web.xml中得配置

[html]
view plaincopy

<!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>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!--

<context-param>

<param-name>resteasy.scan.resources</param-name>
<param-value>true</param-value>
</context-param>
-->
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

相比原来的配置文件多了

[html]
view plaincopy

<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>



[html]
view plaincopy

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

3.application.xml的配置

[html]
view plaincopy

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<!-- service point -->
<bean id="testServicePoint" class="com.jd.interfacce.service.point.TestServicePoint">
<property name="manager" ref="manager" />
</bean>
<!-- MANAGER -->
<bean id="manager" class="com.jd.interfacce.manager.impl.ManagerImpl"/>
</beans>

其中

[html]
view plaincopy

testServicePoint

是我们的资源resouce也就是发布接口的类

4.资源类

[java]
view plaincopy

package com.jd.interfacce.service.point;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import com.jd.interfacce.manager.Manager;
/**
* service point endpoint
* @author
*
*/
@Path(value = "/")
public class TestServicePoint {
Manager manager;

@GET
@Path(value = "/echo/{message}")
public String getMessage(@PathParam(value = "message") String message)
{
// return message;
return manager.getMessage(message);
}

public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
}

5.manager类

[java]
view plaincopy

package com.jd.interfacce.manager.impl;

import com.jd.interfacce.manager.Manager;

public class ManagerImpl implements Manager{
@Override
public String getMessage(String message)
{
return message;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐