spring集成resteasy实例

resteasy 是 jboss的一个开源restful service实现参考。

项目结构:

这里写图片描述
本实例开发环境:intelliJ idea、jdk1.7 ,tomcat7

项目构建步骤

1、新建maven项目RestfulDemo

2、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>RestfulDemo</groupId>
    <artifactId>RestfulDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <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>


</project>

3、web.xml

<!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*:spring/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>

4、ApplicationContext.xml

<?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.rest.demo.manager.point.TestServicePoint">
        <property name="manager" ref="manager" />
    </bean>
    <!-- MANAGER -->
    <bean id="manager" class="com.rest.demo.manager.impl.ManagerImpl"/>
</beans>

5、Manager接口

package com.rest.demo.manager;

/**
 * @author someone
 * @create 2017-10-09 15:28
 **/
public interface Manager {
    public String getMessage(String message);
}

6、ManagerImpl类

package com.rest.demo.manager.impl;

import com.rest.demo.manager.Manager;

/**
 * @author someone
 * @create 2017-10-09 15:29
 **/
public class ManagerImpl implements Manager {
    public String getMessage(String message) {
        return message;
    }
}

7、TestServicePoint测试类

package com.rest.demo.manager.point;

import com.rest.demo.manager.Manager;

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

/**
 * @author someone
 * @create 2017-10-09 15:32
 **/
@Path(value = "/")
public class TestServicePoint {
    Manager manager;

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

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

到这里,项目就已经写完啦!

项目部署:

1、将项目部署到tomcat
2、启动tomcat
3、访问rest接口
如:http://localhost:8188/rest/echo/hello
返回:hello
如:http://localhost:8188/rest/echo/123
返回:123

关于rest相关知识可以参考:http://blog.csdn.net/sweetgirl520/article/details/78181055
本文参考:http://lib.csdn.net/article/java/2451

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值