http接口的调用于被调用的简单实现

接口别人来调用
@RequestMapping (value= "/sendMsg" , method= RequestMethod. POST , consumes = CONSUMES )
public void sendMsg( @RequestBody Map<String,String> form ,HttpServletResponse response ){
  JSONObject json = new JSONObject();
  String phones = form .get( "phone" );
  String content = form .get( "content" );
   Msg msg = new Msg();
   String[] phoneArr = phones .split( "," );
   for ( int i =0; i < phoneArr . length ; i ++){
      String phone = phoneArr [ i ];
      msg .setContent( content );
      msg .setSendTime( new Date());
      msg .setPhone( phone );
      int num = msgServcie .insertMsg( msg );    
   }
      json .put( "resultCode" , CODE_003 );
      json .put( "msg" , CODE_000_MSG );
      outputJson( response , json );
      return ;
   
  
}
private void outputJson(HttpServletResponse response , JSONObject json ) {
  response .setCharacterEncoding( "UTF-8" );
  response .setContentType( "text/javascript;charset=UTF-8" );
  response .setHeader( "Cache-Control" , "no-store, max-age=0, no-cache, must-revalidate" );
  response .addHeader( "Cache-Control" , "post-check=0, pre-check=0" );
  response .setHeader( "Pragma" , "no-cache" );
  try {
    PrintWriter out = response .getWriter();
    try {
      out .write( json .toString());
    } finally {
      out .close();
    }
  } catch (IOException e ) {
    e .printStackTrace();
  }
}
// 调用别人接口
    
     @RequestMapping ( "/updateInterface.do" )
     @ResponseBody
     public void updateInterface(HttpServletResponse response , HttpSession httpSession , String dialogueSign ,String termSign ) {
         CloseableHttpClient client = HttpClients. createDefault ();
         String sn = "A10164900046" ;
         String account = httpSession .getAttribute( "account" ).toString();
         HttpPost httpPost = new HttpPost(UtilConstants. ROBOT_INTERFACE_URL );
         List< NameValuePair > formparams = new ArrayList< NameValuePair >();
          formparams .add( new BasicNameValuePair( "account" , account ));
          formparams .add( new BasicNameValuePair( "sn" , sn ));
         UrlEncodedFormEntity uefEntity = null ;
          try {
              uefEntity = new UrlEncodedFormEntity( formparams );
         } catch (UnsupportedEncodingException e ) {
              e .printStackTrace();
         }
          httpPost .setEntity( uefEntity );
         CloseableHttpResponse closeresponse = null ;
          try {
              closeresponse = client .execute( httpPost );
         } catch (IOException e ) {
              e .printStackTrace();
         }
         HttpEntity entity = closeresponse .getEntity();
         String result = null ;
          try {
              result = EntityUtils. toString ( entity , "UTF-8" );
         } catch (org.apache.http.ParseException | IOException e ) {
              e .printStackTrace();
         }
         Map<String, String> rsMap = new HashMap<String, String>();
          Map map = JSON. parseObject ( result );
         String rs = (String) map .get( "result" );
         String ms = (String) map .get( "msg" );
          if ( rs .equals( "0" )) {
             RobotUpdate robotUp = new RobotUpdate();
              robotUp .setUser( account );
              robotUp .setSn( sn );
              if ( dialogueSign != null && dialogueSign .equals( "1" )) {
                  robotUp .setDialogueSign( "0" );
             }
              if ( termSign != null && termSign .equals( "1" )) {
                  robotUp .setTermSign( "0" );
             }
              robotUpdateService .updateRobot( robotUp );
              rsMap .put( "code" , "0" );
         } else {
              if ( ms .equals( "get parameter error,no user or sn" )) {
                  rsMap .put( "msg" , "-1" );
             }
              if ( ms .equals( "Open mysql error!" )) {
                  rsMap .put( "msg" , "-2" );
             }
              if ( ms .equals( "Get user version error!" )) {
                  rsMap .put( "msg" , "-3" );
             }
              if ( ms .equals( "Reade or write questions and answers to sqlite error!" )) {
                  rsMap .put( "msg" , "-4" );
             }
              if ( ms .equals( "Read or write user professional vocabulary error!" )) {
                  rsMap .put( "msg" , "-5" );
             }
              if ( ms .equals( "ead or write config message error!" )) {
                  rsMap .put( "msg" , " -6" );
             }
              if ( ms .equals( "Database encrypt error!" )) {
                  rsMap .put( "msg" , "-7" );
             }
              if ( ms .equals( "Patch error!" )) {
                  rsMap .put( "msg" , "-8" );
             }
              if ( ms .equals( "Write patch message error!" )) {
                  rsMap .put( "msg" , "-8" );
             }
         }
         JSONObject jsonObject = JSONObject. fromObject ( rsMap );
          try {
              response .getWriter().print( jsonObject .toString());
         } catch (IOException e ) {
              e .printStackTrace();
         }
    }
}
maven 依赖
< dependency >
              < groupId > org.apache.httpcomponents </ groupId >
              < artifactId > httpclient </ artifactId >
              < version > 4.5 </ version >
          </ dependency >
          < dependency >
              <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpcore</artifactId>
             <version>4.4.1</version>
          </ dependency >

猜你喜欢

转载自blog.csdn.net/isHarry/article/details/80536389