spring mvc完美支持restful的put提交方式

spring mvc resuful中,put方法好像支持的不太好,不管用註解仍是request.getParameter等方法都獲取不到,我在網上找了一段代碼,修改了下,能夠完美得到參數.java

方式1:web

方式2:spring

PageData是用request封裝的,有興趣的能夠問我要代碼。mvc

 

下面看對put的封裝:app

import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class PutRequestWrapper extends HttpServletRequestWrapper {

	// 自定義解析完成後的參數,放入request中的key

	public final static String CUSTOMER_ATTR_KEY = "customer.attribute.parameter";

	private Map<String, String[]> reqParams;

	public PutRequestWrapper(HttpServletRequest request) {
		super(request);
		reqParams = (Map<String, String[]>) request.getAttribute(CUSTOMER_ATTR_KEY);
	}

	public Map getParameterMap() {
		return reqParams;
	}

	public Enumeration getParameterNames() {
		return Collections.enumeration(reqParams.keySet());
	}

	public String[] getParameterValues(String name) {
		return reqParams.get(name);
	}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @author jacky
 *
 *  2016年7月22日
 */
public class PutMethodFilter implements javax.servlet.Filter {

	/**
	 * 
	 * Default constructor.
	 * 
	 */
	public PutMethodFilter() {

	}

	/**
	 * 
	 * @seeFilter#destroy()
	 * 
	 */
	public void destroy() {

	}

	/**
	 * 
	 * @seeFilter#doFilter(ServletRequest,ServletResponse, FilterChain)
	 * 
	 */

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		// PUT請求不能通過request.getparameter()獲得參數
		if ("PUT".equalsIgnoreCase(req.getMethod())) {
			// InputStreamReader
			BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
			String org_req_content;
			StringBuilder content = new StringBuilder();
			while ((org_req_content = in.readLine()) != null) {
				content.append(URLDecoder.decode(org_req_content, "UTF-8"));
				Map<String, String[]> map = parseRequestParameter(content.toString());
				request.setAttribute(PutRequestWrapper.CUSTOMER_ATTR_KEY, map);
				// 包裝成自定義的請求
				request = new PutRequestWrapper(req);
				chain.doFilter(request, response);
			}
		}else {
			chain.doFilter(request, response);
		}
	}

	/**
	 * 
	 * 自定義參數解析
	 * 
	 * 注:此方法暫時沒有考慮具備相同名稱的一組提交值的情況,如須要,請本身更改下面的代碼
	 * 
	 * @paramcontent
	 * 
	 * @return
	 * 
	 */
	public Map<String, String[]> parseRequestParameter(String content) {
		Map<String, String[]> params = new HashMap<String, String[]>();
		if (content == null || "".equals(content.trim())) {
			return params;
		}
		try {
			String[] tmp = content.split("&");
			for (int i = 0; i < tmp.length; i++) {
				String[] keyValue = tmp[i].split("=");
				params.put(keyValue[0], new String[] { keyValue[1] });
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}

	public void init(FilterConfig arg0) throws ServletException {

	}
}

web.xmlui

<filter>
		<filter-name>putMethodFilter</filter-name>
		<filter-class>com.xxx.filter.PutMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>putMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
相關文章
相關標籤/搜索