Intellij IDEA环境配置RestEasy,SpringMVC+RestEasy

时间:2023-03-09 16:07:51
Intellij IDEA环境配置RestEasy,SpringMVC+RestEasy

在SpringMvc中配置RestEasy,需要以下步骤

1、通过maven导入restEasy所需要的jar包

2、在web.xml文件中添加相应的配置。

3、编写服务。

具体步骤:

1、通过maven导入restEasy所需要的jar包,找到pom.xml文件(我自己的项目是加入到了web下的pom.xml文件中),添加下列代码,引入相应的jar包

 <!--resteasy相关jar包引入 -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.7.Final</version>
</dependency>
<!-- Reasteasy 集成Spring g工具包-->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.7.Final</version>
</dependency> <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.7.Final</version>
</dependency>

jar导入之后,会在External Libraries中显示导入的jar包,如果未显示,说明jar没有正常导出;

2、web.xml中对resteasy的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:spring.xml</param-value>
</context-param> <!--为resteasy指定JSR-RS服务,代表通过指定资源类的全路径名进行加载,有多个资源类可通过逗号分开 -->
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>org.hc.heaton.product.controller.AppResteasyTestController,org.hc.heaton.app.controller.AppDoLoginController</param-value>
</context-param>
<!--要指定resteasy服务的前缀,否则和spring mvc的url-pattern冲突 -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/appServer</param-value>
</context-param>
<!-- 防止发生java.beans.Introspector内存泄露,应将它配置在ContextLoaderListener的前面 -->
<!-- 详细描述见http://blog.****.net/jadyer/article/details/11991457 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <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> <!--&lt;!&ndash; 实例化Spring容器 &ndash;&gt;-->
<!--&lt;!&ndash; 应用启动时,该监听器被执行,它会读取Spring相关配置文件,其默认会到WEB-INF中查找applicationContext.xml &ndash;&gt; -->
<!--<listener>-->
<!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
<!--</listener> --> <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/views/city/*</url-pattern>
</filter-mapping> <!-- 配置Shiro过滤器,先让Shiro过滤系统接收到的请求 -->
<!-- 这里filter-name必须对应applicationContext.xml中定义的<bean id="shiroFilter"/> -->
<!-- 使用[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 -->
<!-- 通常会将此filter-mapping放置到最前面(即其他filter-mapping前面),以保证它是过滤器链中第一个起作用的 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置分页过滤器 -->
<!-- 使用[/*]匹配所有请求,保证所有的可控请求都经过Shiro的过滤 -->
<!-- 通常会将此filter-mapping放置到最前面(即其他filter-mapping前面),以保证它是过滤器链中第一个起作用的 -->
<filter>
<filter-name>PageFilter</filter-name>
<filter-class>org.workholic.framework.common.ligeruiUtils.ServerPageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PageFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- SpringMVC核心分发器-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--<param-value>/WEB-INF/classes/spring-mvc.xml</param-value>-->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--resteasy服务配置 -->
<servlet>
<servlet-name>resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!--resteasy访问路径映射 -->
<servlet-mapping>
<servlet-name>resteasy</servlet-name>
<url-pattern>/appServer/*</url-pattern>
</servlet-mapping>

<error-page>
<error-code>500</error-code>
<location>/errorpage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errorpage.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
</web-app>

3、编写服务

package org.hc.heaton.app.controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hc.heaton.app.service.AppDoLoginService;
import org.hc.heaton.system.member.entity.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.workholic.framework.common.utils.UtilsJson; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map; /**
* Created by py on 2016/10/26.
* Description:App登录接口
*/
@Path("/rest")
public class AppDoLoginController {
@Autowired
AppDoLoginService appDoLoginService;
/**
* 接口登录
* @param cookie sid值(唯一标示)
* @param postData 格式:{"username":"001","password":"dfdfd"}
* @return
*/
@POST
@Path("/doLogin")
@Consumes({MediaType.APPLICATION_JSON}) //接口接受到的参数类型
@Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8"}) //接口返回格式为json,返回编码格式为utf-8(解决中文乱码问题)
public String doLogin(@HeaderParam("cookie") String cookie,String postData){
ResultEntity resultEntity=new ResultEntity();
Map<String,Object> postMap=null;
if(!cookie.equals("") && !postData.equals("")){
postMap= UtilsJson.toMap(postData);//将Json转换成Map
resultEntity=appDoLoginService.doLogin(postMap,cookie);
}
else {
resultEntity.setSuccess(false);
resultEntity.setMessage("用户名和密码不能为空,cookie值不能为空");
}
String mess=UtilsJson.toJson(resultEntity);
return mess;
}
}

特别说明:

@HeaderParam("cookie") String cookie参数可以获得请求头中的Cookie信息(Sid编码)

String postData参数传过来是Json格式的字符串,可以转换;如果参数是个实体类型,那resteasy可以自动转换
通过搜狐浏览器中的HttpRequester可以测试接口