搭个简单Jersey+spring工程

时间:2021-03-28 19:34:40

1 导入Jersey spring 解析json必要的jar包

jersey-client-1.8.jar

jersey-core-1.8.jar

jersey-json-1.8.jar

jersey-server-1.8.jar

jersey-spring-1.8.jar


2 web.xml部分配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- 1 创建Spring的监听器,监听下列配置的请求 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 2 Spring 的监听器可以通过这个上下文参数来获取beans.xml的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans.xml</param-value>
</context-param>

<!-- 3 Spring 截取请求,将其分发到配置对应的类中,做页面处理和中转,对应文件springmvc-servlet.xml -->
<!-- <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/deal/*</url-pattern>
</servlet-mapping>
-->
<!-- 4 Spring 过滤器,统一字符集编码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Jersey Servlet 配置 -->
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.full.control</param-value> <!-- 对应自己配置有Jersey注解的类 -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3 Jsp中的表单:

<body>
<br/>-----------------restful login------------------------------
<form name = 'form1' action = "${path}/rest/rest/getStuinfos" method = 'get'>
name:
<input type="text" name="username" value = 'wang'/>
<br />
pwd :
<input type="text" name="password" value = 'pwd'/><br/>
<input type="submit" value="sub btn"><br/>
</form>
</body>


4 Jersey的服务类:

package com.taikang.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.taikang.bean.StuInfo;
import com.taikang.service.ISchoolService;

@Controller
@Path("rest")
public class RestControl {

@Resource
private ISchoolService schoolServiceImpl;

@Path("getStuinfos")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getStuInfos(@PathParam("username") String username,
@Context HttpServletRequest request)
{
String info = "sss";
String sname = request.getParameter("username");
List<StuInfo> stuInfos = schoolServiceImpl.getStuInfos(sname); //此处是通过mybatis配置的,可以自己的类
info = JSON.toJSONString(stuInfos.get(0));
return info;
}

}


有的框架整合了spirng-mvc和Jersey,这两儿如果都单独在web.xml里边配置的有,则Jersey对象可能就获取不到spring应该注入的bean。

使用json形式交互是,可以参考:

http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html