SpringMVC获取HttpClient 请求的数据

package com.nnk.upstream.controller;


import org.springframework.util.StreamUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Created with IntelliJ IDEA.
* User: y
* Date: 2016/6/12
* Time: 9:30
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
private byte[] body;
public MyHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
body = StreamUtils.copyToByteArray(request.getInputStream());
String contentType = request.getContentType();

System.out.println("contentType:" + contentType);
System.out.println("body length:" + body.length);
}

@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}

@Override
public boolean isFinished() {
return false;
}

@Override
public boolean isReady() {
return false;
}

@Override
public void setReadListener(ReadListener readListener) {

}
};

}

@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
}

[quote]过滤器配置[/quote]
package com.nnk.upstream.controller;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
* Created with IntelliJ IDEA.
* User: y
* Date: 2016/6/12
* Time: 9:35
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
@WebFilter(filterName = "HttpReplaceFilter",urlPatterns="/*")
public class HttpReplaceFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;

if(request instanceof HttpServletRequest) {
if(((HttpServletRequest) request).getMethod().equalsIgnoreCase("post")) {
requestWrapper = new MyHttpServletRequestWrapper((HttpServletRequest) request);
}
}
if(null == requestWrapper) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}

@Override
public void destroy() {

}
}

[quote]springboot 对过滤器的支持需要使用@ServletComponentScan相关注解[/quote]

@Configuration
@ComponentScan("com.nnk.upstream")
@EnableAutoConfiguration
@ImportResource("classpath:/context/bean.xml")
@ServletComponentScan
public class SampleIntegrationApplication extends SpringBootServletInitializer {
public static final Logger LOGGER = Logger.getLogger(SampleIntegrationApplication.class);
public static String IntanceKey;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleIntegrationApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleIntegrationApplication.class, args);
}
}

[quote]使用httpClient请求Url会发现无法获取到流数据,如果是contentType:application/x-www-form-urlencoded; charset=UTF-8但是如果是contentType:text/plain; charset=GBK则可以获取流数据。流数据只能被读取一次,而且跟请求类型有关系[/quote]

package com.nnk.redis;

import com.nnk.upstream.core.ConfigContextManager;
import com.nnk.upstream.entity.parterner.CallbackRequest;
import com.nnk.upstream.util.ReflectUtils;
import com.nnk.upstream.util.SignSecurityUtlis;
import com.nnk.upstream.vo.InterfaceConfig;
import com.nnk.utils.http.exception.NetWorkException;
import com.nnk.utils.http.interfaces.HttpData;
import com.nnk.utils.http.utils.HttpClientUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.nio.charset.Charset;

/**
* Created with IntelliJ IDEA.
* User: y
* Date: 2016/4/25
* Time: 9:49
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:context/bean.xml" })
public class CallbackTest {
private Logger logger = Logger.getLogger(CallbackTest.class);
@Autowired
private HttpClientUtils httpClientUtils;
@Autowired
private ConfigContextManager configContextManager;
@Test
public void testCallbackSuccess() throws NetWorkException {

CallbackRequest request = new CallbackRequest();
request.setOrderStatus("SUCCESS");
request.setRespCode("0000");
request.setRealValue("100");
request.setOemorderId("oemH89898709909");
request.setSendorderId("29038297439284320");
request.setMerchantNo("2000000011");
InterfaceConfig config = configContextManager.searchConfigContext("20000009");
request.setRespMsg("成功");
String signStr = ReflectUtils.getkeyValueString(request,"=","&","sign");
String sign = SignSecurityUtlis.sign(signStr,"123456");
request.setSign(sign);
String Resp = httpClientUtils.doPost("http://localhost:8099/Upstream/coustomizeCallback?merid=200000009",request);

}
@Test
public void testCallbackSuccess1() throws NetWorkException {


String Resp = httpClientUtils.doPost("http://localhost:8099/Upstream/coustomizeCallback?merid=200000009",new HttpData() {
@Override
public HttpEntity getPostdata() {
return new StringEntity("a=a&b=b", Charset.defaultCharset());
}

@Override
public String getGetdata() {
return null;
}
});

}
}


[quote]关于SpringMVc获取参数:请参看:http://json20080301.iteye.com/blog/1874074?utm_source=tuicool;
http://hw1287789687.iteye.com/blog/2199295[/quote]
原文地址:https://www.cnblogs.com/lameclimber/p/10842012.html