web.xml中:
<!-- 用户put提交参数 --> <filter> <filter-name>HttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.</filter-class> </filter> <filter-mapping> <filter-name>HttpMethodFilter</filter-name> <servlet-name></servlet-name> </filter-mapping>
<servlet> <servlet-name></servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/conf/*.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Controller中:
@RequestMapping(value="/{userId}",method = RequestMethod.PUT)
测试的main方法中:
String putStr="http://192.168.1.8:8080/xxx/users/36"; try { HttpPut method = new HttpPut(putStr); method.setHeader("serverType", "0"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("orgId", "10")); params.add(new BasicNameValuePair("userName", "bbbddd_001")); params.add(new BasicNameValuePair("email", "liwy@xxx.cn")); params.add(new BasicNameValuePair("phoneNo", "111222")); method.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = client.execute(method); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); System.out.println(result); } catch (IOException e) { e.printStackTrace(); }
客户端进行http请求时用到的jar
ezmorph-1.0.6.jar fluent-hc-4.5.1.jar httpclient-4.5.1.jar httpclient-cache-4.5.1.jar httpclient-win-4.5.1.jar httpcore-4.4.3.jar httpmime-4.5.1.jar jna-4.1.0.jar jna-platform-4.1.0.jar