Temp

来源:互联网 发布:淘宝css滚动栏 编辑:程序博客网 时间:2024/05/18 03:52

CXF
/**
* 自定义扩展Struts2过滤器,用于解决struts会拦截cxf webservice请求的问题
*
*/
public class ExtendStrutsPrepareFilter extends StrutsPrepareFilter {
private static final Logger logger = LoggerFactory.getLogger(ExtendStrutsPrepareFilter.class);

@Override  public void doFilter(ServletRequest req, ServletResponse res,          FilterChain chain) throws IOException, ServletException {      try {          HttpServletRequest request = (HttpServletRequest) req;          // 判断是否是向WebService发出的请求          if (request.getRequestURI().contains("/interface")) {              // 如果是来自向CXFService发出的请求              chain.doFilter(req, res);          } else {              // 默认action请求              super.doFilter(req, res, chain);          }      } catch (Exception e) {          logger.error(e.getMessage());          e.printStackTrace();      }  }  

}

HttpClient
cxf
package com.admin.util;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

public static String doGet(String url, Map<String, String> param) {    // 创建Httpclient对象    CloseableHttpClient httpclient = HttpClients.createDefault();    String resultString = "";    CloseableHttpResponse response = null;    try {        // 创建uri        URIBuilder builder = new URIBuilder(url);        if (param != null) {            for (String key : param.keySet()) {                builder.addParameter(key, param.get(key));            }        }        URI uri = builder.build();        // 创建http GET请求        HttpGet httpGet = new HttpGet(uri);        // 执行请求        response = httpclient.execute(httpGet);        // 判断返回状态是否为200        if (response.getStatusLine().getStatusCode() == 200) {            resultString = EntityUtils.toString(response.getEntity(), "UTF-8");        }    } catch (Exception e) {        e.printStackTrace();    } finally {        try {            if (response != null) {                response.close();            }            httpclient.close();        } catch (IOException e) {            e.printStackTrace();        }    }    return resultString;}public static String doGet(String url) {    return doGet(url, null);}public static String doPost(String url, Map<String, String> param) {    // 创建Httpclient对象    CloseableHttpClient httpClient = HttpClients.createDefault();    CloseableHttpResponse response = null;    String resultString = "";    try {        // 创建Http Post请求        HttpPost httpPost = new HttpPost(url);        // 创建参数列表        if (param != null) {            List<NameValuePair> paramList = new ArrayList<>();            for (String key : param.keySet()) {                paramList.add(new BasicNameValuePair(key, param.get(key)));            }            // 模拟表单            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);            httpPost.setEntity(entity);        }        // 执行http请求        response = httpClient.execute(httpPost);        resultString = EntityUtils.toString(response.getEntity(), "utf-8");    } catch (Exception e) {        e.printStackTrace();    } finally {        try {            response.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    return resultString;}public static String doPost(String url) {    return doPost(url, null);}public static String doPostJson(String url, String json) {    // 创建Httpclient对象    CloseableHttpClient httpClient = HttpClients.createDefault();    CloseableHttpResponse response = null;    String resultString = "";    try {        // 创建Http Post请求        HttpPost httpPost = new HttpPost(url);        // 创建请求内容        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);        httpPost.setEntity(entity);        // 执行http请求        response = httpClient.execute(httpPost);        resultString = EntityUtils.toString(response.getEntity(), "utf-8");    } catch (Exception e) {        e.printStackTrace();    } finally {        try {            response.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    return resultString;}/*添加鉴权请求 * public static String doPostJson(String url, String json) {    // 创建Httpclient对象    CloseableHttpClient httpClient = HttpClients.createDefault();    CloseableHttpResponse response = null;    String resultString = "";    try {        // 创建Http Post请求        HttpPost httpPost = new HttpPost(url);        // 创建请求内容        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);        httpPost.setEntity(entity);        // 执行http请求        response = httpClient.execute(httpPost);        resultString = EntityUtils.toString(response.getEntity(), "utf-8");    } catch (Exception e) {        e.printStackTrace();    } finally {        try {            response.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    return resultString;}*/

}

package com.admin.servlet;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import org.springframework.web.bind.annotation.RequestParam;@Path(value = "/")@Produces({"application/json;charset=UTF-8", "application/xml;charset=UTF-8",    "text/javascript;charset=UTF-8", "text/plain;charset=utf-8"})public interface AppInterefaceService {    /**     * 查询分页信息     * @param queryParams     * @return     */    @POST    @Path("app/queryPage")    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})    public String appQuery(@RequestParam("queryParams") String queryParams);package com.admin.servlet.impl;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.CopyOnWriteArrayList;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.admin.dao.IpccDao;import com.admin.model.PageContainer;import com.admin.servlet.AppInterefaceService;import com.admin.util.HttpClientUtil;import com.admin.util.JsonUtils;import com.admin.util.StringUtils;import com.admin.util.SysConfig;import com.admin.util.ZookeeperUtil;import com.admin.util.log.AddOperateLogUtil;import com.admin.util.web.AuthorityUtils;import com.admin.util.web.StrutsUtils;@Service("AppInterefaceServiceImpl")public class AppInterefaceServiceImpl implements AppInterefaceService{    private static final Logger logger = LogManager.getLogger(AppInterefaceServiceImpl.class);    @Autowired    private IpccDao ipccDao;    @Override    public String appQuery(String queryParams) {        Map<String, Object> paramsMap = JsonUtils.toMap(queryParams);        //超级管理员判断        if((!StringUtils.isEmpty(paramsMap.get("sid").toString()))){            String sid = paramsMap.get("sid").toString();            if(SysConfig.getInstance().getProperty("sid").equals(sid)){                paramsMap.remove("sid");            }        }        PageContainer searchPage = ipccDao.getSearchPage("appConfig.queryPage", "appConfig.queryCount", paramsMap);        return JsonUtils.toJson(searchPage);    }<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd          http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd        ">    <bean id="LoggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />    <bean id="LoggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />    <bean id="CxfResponseInterceptor" class="com.admin.util.interceptor.CxfResponseInterceptor" />    <bean id="SimpleConverter" class="org.codehaus.jettison.mapped.SimpleConverter" />    <bean id="JSONProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">        <property name="typeConverter" ref="SimpleConverter" />        <property name="arrayKeys" value="memberName" />    </bean>    <jaxrs:server id="asServiceContainer" address="/">        <jaxrs:serviceBeans>            <ref bean="AppInterefaceServiceImpl"/>            <ref bean="CallInterefaceServiceImpl"/>            <ref bean="ClientInterefaceServiceImpl"/>            <ref bean="CbInterefaceServiceImpl"/>            <ref bean="IvrInterefaceServiceImpl"/>            <ref bean="GroupInterefaceServiceImpl"/>            <ref bean="SeatInterefaceServiceImpl"/>            <ref bean="RecoverDateInterefaceServiceImpl"/>        </jaxrs:serviceBeans>        <!-- 输入日志拦截器 -->        <jaxrs:inInterceptors>            <ref bean="LoggingInInterceptor" />        </jaxrs:inInterceptors>        <!-- 输出拦截器 -->        <jaxrs:outInterceptors>            <!-- 输出日志拦截器 -->            <ref bean="LoggingOutInterceptor" />            <!-- 输出响应拦截器 -->            <ref bean="CxfResponseInterceptor" />        </jaxrs:outInterceptors>        <jaxrs:providers>            <ref bean="JSONProvider" />        </jaxrs:providers>        <jaxrs:extensionMappings>            <entry key="json" value="application/json" />            <entry key="xml" value="application/xml" />        </jaxrs:extensionMappings>        <jaxrs:languageMappings>            <entry key="en" value="en-gb" />        </jaxrs:languageMappings>    </jaxrs:server></beans>这里写代码片
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 六年级的数学下册差怎么办 一年级小孩做作业慢怎么办 静不下心写作业怎么办 二年级应用题太差怎么办 小学二年级数学差怎么办 小学二年级成绩差怎么办 6个月小孩爱动怎么办 儿子叛逆期我该怎么办 宝宝两岁好动不听话怎么办 生宝宝后奶水少怎么办 生了孩子没出来怎么办 孩子在学校表现不好怎么办 3岁半宝宝话太多怎么办 孩子不喜欢和小朋友玩怎么办 孩子不喜欢和小朋友说话怎么办 4岁半宝宝不听话怎么办 小孩在学校打老师怎么办 老师老找孩子时怎么办 幼儿园老师批评孩子后家长怎么办 老师跟家长吵架了怎么办 孩子在幼儿园被老师孤立怎么办 学生在幼儿园被老师欺负怎么办 小孩脚痒怎么办小窍门 小孩肚子病怎么办天天说 幼儿园幼儿信息表填错了怎么办 水浒传书孩子说看不懂怎么办 孩子丢了书老师怎么办 小朋友做错事不承认老师怎么办 教师被学生骂后怎么办 嘴吧里面长泡怎么办 有个小孩怕下雨怎么办? 幼儿的家长打我怎么办 老师打学生被家长起诉怎么办 家长在学校打了老师怎么办 老师打小孩我们家长怎么办呢? 孩子长手、腿毛怎么办 腿毛又黑又多怎么办 孩子怕老师说他怎么办 被老师骂到厌学怎么办 孩子对写作业一点也不主动怎么办 高三孩子太贪玩怎么办