spring4.x 集成 jersey2.2x 提供资源服务,以restful 风格

开发环境这里就滤过:

说重点

1.整个工程目录结构:

简单说明一下: resources 向外部提供的资源,service 包里有 service ,serviceimpl 实现类;

详细步骤主要是配置:

1.动态创建一个动态Web工程;

   找到依赖jar包:

 

2.配置web.xml

<display-name>RestDemo3</display-name>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

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

    <!-- 配置Jersey+Spring -->
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- 配置默认的资源包路径,使用默认的配置类去加载资源 
       <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.ieds.rest</param-value>
        </init-param> 
       -->        
       <!-- 配置自己的资源加载类去加载资源 -->
        <init-param> 
                   <param-name>javax.ws.rs.Application</param-name> 
                   <param-value>com.ieds.rest.RestApplication</param-value> 
        </init-param>  
       
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

3.配置application-context.xml

   <context:component-scan base-package="com.ieds.rest.*" /> <!-- 扫描该类下所有含有@component,@service,@Repository -
   

4、

5、对外提供资源服务的资源类;

package com.ieds.rest.service;

import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl implements StudentService {


	@Override
	public void doSomething(String msg) {
		
		System.out.println("do some thing:" + msg);
		
	}

}
package com.ieds.rest.resources;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.ieds.rest.service.StudentService;
/**
 * IE测试: http://localhost:8080/RestDemo2/student/resource1?msg=helloworld
 * @author yangrenjun
 *
 */
@Component
@Path("student")
public class StudentResource {


    @Autowired
    private StudentService studentService;

	@Autowired
    private ApplicationContext ctx;

	
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

   

    @Path("resource1")
    @GET
    public String resource1(@QueryParam("msg") String msg) {
        System.out.println("当前这个对象:"+this);
        System.out.println("从容器里获取当前对象:"+ctx.getBeansOfType(StudentResource.class));
        this.studentService.doSomething(msg);
        return "success";
    }
}

 

6、通过浏览器输入:http://localhost:8080/RestDemo3/student/resource1?msg=helloworld

 

7、完成

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值