开发环境这里就滤过:
说重点
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、完成