zoukankan      html  css  js  c++  java
  • 【Pyton】访问Java提供的Rest接口示例

    代码:

    # coding=utf-8
    import urllib.request
    
    url="http://localhost:8080/mediacool/probeUser"
    
    param_dict={'name':'何杨','pswd':'1'}
    param_str=urllib.parse.urlencode(param_dict)
    params_bytes=param_str.encode()
    
    req=urllib.request.Request(url,data=params_bytes)
    with urllib.request.urlopen(req) as rsps:
        data=rsps.read()
        json_data=data.decode();
        print(json_data)

    输出:

    C:hypy>python postMediaCoolUser.py
    {"Interface ID":"8973","startTime":"2021-10-31 12:08:33.793817800","visitorIp":"0:0:0:0:0:0:0:1","code":"303","msg":"确 认用户名和密码无误","userInfo":{"id":1,"name":"何杨","email":"1@2.3"},"endTime":"2021-10-31 12:08:33.799822200"}

    附:

    Java接口:

    package com.hy.myapp.rest;
    
    import java.time.LocalDate;
    import java.time.LocalTime;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.hy.myapp.Entity.User;
    import com.hy.myapp.service.UserService;
    import com.hy.myapp.util.RestUtil;
    
    /**
     * 用于用户服务的Restful控制器
     * @author hy
     * 2021年10月31日
     */
    @RestController
    public class UserRestCtrl {
        @Autowired
        private UserService userService;
        
        @RequestMapping(value="/probeUser", method=RequestMethod.POST)
        public Map<String,Object> probeUser(String name,String pswd,HttpServletRequest rqst){
            Map<String,Object> retvalMap=new LinkedHashMap<String,Object>();
            
            retvalMap.put("Interface ID", "8973");
            
            String startTime=LocalDate.now()+" "+LocalTime.now();
            retvalMap.put("startTime", startTime);
            
            String visitorIp=RestUtil.findVisitorIpFrom(rqst);
            retvalMap.put("visitorIp", visitorIp);
            
            if(name==null) {
                retvalMap.put("code", "301");
                retvalMap.put("msg", "name不可缺失");
                String endTime=LocalDate.now()+" "+LocalTime.now();
                retvalMap.put("endTime", endTime);
                return retvalMap;
            }
            
            if(pswd==null) {
                retvalMap.put("code", "302");
                retvalMap.put("msg", "password不可缺失");
                String endTime=LocalDate.now()+" "+LocalTime.now();
                retvalMap.put("endTime", endTime);
                return retvalMap;
            }
            
            // 到此name、pswd都有了
            User user=userService.searchUserByNamePswd(name, pswd);
            if(user!=null) {
                retvalMap.put("code", "303");
                retvalMap.put("msg", "确认用户名和密码无误");
                retvalMap.put("userInfo", user);
                
                String endTime=LocalDate.now()+" "+LocalTime.now();
                retvalMap.put("endTime", endTime);
                
                return retvalMap;
            }else {
                retvalMap.put("code", "304");
                retvalMap.put("msg", "用户名或密码有误");
                
                String endTime=LocalDate.now()+" "+LocalTime.now();
                retvalMap.put("endTime", endTime);
                
                return retvalMap;
            }
        }
    }

    END

  • 相关阅读:
    js对象数组中的某属性值 拼接成字符串
    mongodb 简单的增删改查
    celery简单使用
    Redis简单操作
    三大认证源码分析
    认证、权限、频率、自定义签发token-多方式登录
    jwt认证
    视图家族
    day75作业
    day74作业
  • 原文地址:https://www.cnblogs.com/heyang78/p/15488994.html
Copyright © 2011-2022 走看看