Browse Source

解决乱码问题

chenweida 9 years ago
parent
commit
4475aba723

+ 32 - 0
Hos-Resource-Rest/src/main/java/com/yihu/hos/filter/CharFilter.java

@ -0,0 +1,32 @@
package com.yihu.hos.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * Created by Administrator on 2016/4/26.
 */
@Component("charFilter")
public class CharFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {
    }
}

+ 24 - 6
Hos-Resource-Rest/src/main/java/com/yihu/hos/gateway/control/GatewayControl.java

@ -12,17 +12,23 @@ import com.yihu.hos.resource.viewModel.SQLResponResult;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
/**
 * Created by chenweida on 2016/1/27.
 * EHR平台应用 REST请求
 */
@RestController
@Controller
@RequestMapping("/gateway")
public class GatewayControl {
    @Autowired
@ -43,8 +49,15 @@ public class GatewayControl {
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/transfer")
    public Object transfer(HttpServletRequest request) {
    public void transfer(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        Writer writer = response.getWriter();
        response.setCharacterEncoding("UTF-8");
        String returnString = "";
        boolean isSyn = false;//是否异步
        String resultData = null;
        RestRequsetResult restRequsetResult = new RestRequsetResult();
@ -72,22 +85,27 @@ public class GatewayControl {
                        resourceRestDetailModel.getPassword(),
                        JSONObject.fromObject(resourceRestDetailModel.getParam()),
                        resourceRestDetailModel.getType())).start();
                return new SQLResponResult();
                returnString = JSONObject.fromObject(new SQLResponResult()).toString();
            } else {
                resultData = getResultData(resourceRestDetailModel, restRequsetResult.getParam());
                returnString = resultData;
            }
            return resultData;
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof EHRException) {
                //我们自己定义的异常,业务出错
                EHRException eHRException = (EHRException) e;
                return RestResponseResult.getError(eHRException.getExceptionCode(), eHRException.getExceptionMessage());
                returnString = JSONObject.fromObject(RestResponseResult.getError(eHRException.getExceptionCode(), eHRException.getExceptionMessage())).toString();
            } else {
                //系统的异常
                return RestResponseResult.getError(EHRExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION, EHRExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION_MESSAGE);
                returnString = JSONObject.fromObject(RestResponseResult.getError(EHRExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION, EHRExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION_MESSAGE)).toString();
            }
        }
        writer.write(returnString);
        writer.flush();
        writer.close();
    }
    /**

+ 17 - 1
Hos-Resource-Rest/src/main/java/com/yihu/hos/resource/base/App.java

@ -2,8 +2,10 @@ package com.yihu.hos.resource.base;
import com.coreframework.remoting.Server;
import com.yihu.hos.config.Config;
import com.yihu.hos.filter.CharFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
@ -11,7 +13,10 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import java.io.FileOutputStream;
import java.io.InputStream;
@ -30,7 +35,7 @@ import java.util.Set;
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan("com.yihu.hos")
@EntityScan("com.yihu.hos.**.model")
public class App {
public class App extends WebMvcConfigurerAdapter {
    private static Properties prop = new Properties();
    //@RequestMapping("/")
@ -57,6 +62,7 @@ public class App {
        app.setWebEnvironment(true);
        app.setShowBanner(false);
        Set<Object> set = new HashSet<Object>();
        //set.add("classpath:applicationContext.xml");  
        app.setSources(set);
@ -93,6 +99,7 @@ public class App {
        // server.start();
    }
    /**
     * spring boot 定时任务
     */
@ -100,5 +107,14 @@ public class App {
    // public void reportCurrentTime() {
    //System.out.println("抽取数据");
    // }
    // 用于处理编码问题
    @Bean
    public FilterRegistrationBean filterRegistrationBean(CharFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}