SPRINGMVC combined with HttpClient to invoke third-party interface implementations

Source: Internet
Author: User

Using httpclient to rely on jar packages

1:commons-httpclient-3.0.jar

2:commons-logging-1.1.1.jar

3:commons-codec-1.6.jar

Local Call Test ===>>>>>>>>>>>>

 Packagecom.me.cn.siteTrans.test;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.HttpStatus;ImportOrg.apache.commons.httpclient.methods.PostMethod;ImportCom.google.gson.Gson; Public classSitetranctrl { Public Static voidMain (string[] args) {String result=httpclienttest (); SYSTEM.OUT.PRINTLN (result);} Public StaticString httpclienttest () {HttpClient client=NewHttpClient (); String result= "" ; Try{Postmethod Method=NewPostmethod ("Http://localhost:8666/test/sitetrans/returnjson.do?param={test:test}") ;        Client.executemethod (method); if(Method.getstatuscode () = = HTTPSTATUS.SC_OK) {//Successful Responseresult = Method.getresponsebodyasstring ();//Call return result}Else{//unsuccessful assembly resultsmap<string, Object >map =NewHashmap<string, object>(); Gson Gson=NewGson (); Map.put ("Success",false); Result=Gson.tojson (map); }    } Catch(Exception e) {e.printstacktrace (); }    returnresult;}}

Analog third-party interface ===>>>>>>>>>>>>

 PackageCom.cn.me.siteTrans.interface;ImportJava.util.HashMap;ImportJava.util.Map;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportCom.google.gson.Gson, @Controller @requestmapping ("/sitetrans") @SuppressWarnings ("All") Public classSitetrans {@RequestMapping ("/returnjson") @ResponseBody Publicmap Returnjson (String param) {map<string, object> map =NewHashmap<string, object>() ; Try {            System.out.println (param); Map.put ("Success",true) ; Map.put ("Flag",true); } Catch(Exception e) {e.printstacktrace (); }        returnmap; }}

SPRINGMVC combined with HttpClient to invoke third-party interface implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.