栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring+date+jpa springboot 接口传参的几种形式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring+date+jpa springboot 接口传参的几种形式

一、通过Map接收

    @RequestMapping(value = "appLogin", method = RequestMethod.POST)
    @ResponseBody
    public JSonObject appLogin(@RequestBody Map map) {
        JSonObject results=new JSonObject();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                map.get("username").toString(),
                map.get("password").toString(),
                "app");
        String msg = "登录成功!";
        int code = 0;
        String username=map.get("username").toString();
        //进行验证,这里可以捕获异常,然后返回对应信息 
        Map result = new HashMap(); 
        try {
            subject.login(usernamePasswordToken);
            User user =null;
            if(!"".equals(username)){user = accountmanager.findByUsername(username);} 
            List groupsList =user.getGroupsList();
            List maplist=new ArrayList();
            for(Group group:groupsList){
            Map mapsuv=new HashMap();
            mapsuv.put("id", group.getId());
            mapsuv.put("name",group.getName());
            mapsuv.put("type",group.getType());
            maplist.add(mapsuv);
            }
            result.put("groupsList", maplist);
            result.put("userid", user.getId());
            result.put("username", map.get("username").toString());  
        } catch (IncorrectCredentialsException e) {
            msg = "密码不正确"; code = 1;
        } catch (UnknownAccountException e1) {
            msg = "账号不存在"; code = 1;
        } catch (IncorrectCaptchaException e2) {
            msg = "验证码错误"; code = 1;
        }
            results.put("code", code);
            results.put("message", msg);
            results.put("result", result);
        return results;
    }  

二、 通过url 传参

    @ResponseBody
    @RequestMapping("/branch/findOne/{branchId}")
    public String branchFindOne( @PathVariable String branchId) {
        String msg = "error";//返回信息
        int code = 1;
        List list = new ArrayList();
        try {
            list = mainTenanceInterfaceService.getPtBybranchId(Long.parseLong(branchId));
            msg = "success";
            code = 0;
        } catch (Exception e) {
            e.printStackTrace();
        }

        List result = Arrays.asList(list.toArray());
        return JSON.toJSonString(ResponseResult.result(code, msg, result));
    }

三、 通过request 获取json串

    @ResponseBody
    @RequestMapping("/task/overhaulJournal") 
    public void overhaulJournal(
            HttpServletRequest request, HttpServletResponse response) throws IOException { 
        JSonObject result = new JSonObject();
        BufferedReader br;
        StringBuilder sb = new StringBuilder("");
        br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));
        response.setContentType("text/plain;charset=UTF-8");
        String temp;
        while ((temp = br.readLine()) != null) {
            sb.append(temp);
        }
        br.close();
        if (sb != null) {
            String msg = "error";//返回信息
            int code = 1; 
            try {
            //解析构造对象 
                JSonObject obj = JSONObject.parseObject(sb.toString());
                if (obj != null) {    
                    mainTenanceInterfaceService.insertTaskJournal(obj); 
                    msg = "success";//返回信息
                    code = 0;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            result.put("code", code);
            result.put("message", msg);
            result.put("result", "");
            response.getOutputStream().write(result.toString().getBytes(), 0, result.toString().getBytes().length);
        }
    }      

     public void insertTaskJournal(String returnUrl, String path,JSonObject obj){ 
                    String taskJournalstr=obj.getString("taskJournal"); 
                    TaskJournal taskJournal=JSONObject.parseObject(taskJournalstr,TaskJournal.class);
                    taskJournal.setTime(new Date());  
                    TaskJournal journal=taskJournalRepository.save(taskJournal);
                    
                    String questions = obj.getString("questions"); 
                    JSonArray mapList=JSONArray.fromObject(questions);
                    if (mapList != null && !mapList.isEmpty()) {
                    for(int i=0;i                         String questionstr=mapList.getJSonObject(i).getString("question");
                        Question question=JSONObject.parseObject(questionstr,Question.class);
                        question.setTaskJournal(journal);
                        question.setDiscoverTime(new Date());   
                        questionRepository.save(question);  
                         
                        List imageList = (List) JSONArray.toList(JSONArray.fromObject(mapList.getJSonObject(i).get("imageList").toString()), Imgbase64Entity.class);
                        if (imageList != null && !imageList.isEmpty()) {
                        for (Imgbase64Entity ele : imageList) {
                            //base64图片数据
                            String img = ele.getImg();
                            //base64 转成图片,返回图片地址
                            Map map = base64ToByteUtils.savebase64(returnUrl, path, img);
                            //入库
                            String fileName = map.get("fileName");
                            String imgPath = map.get("imgPath");
                            DefectPicture defectPicture = new DefectPicture();
                            defectPicture.setCreateTime(new Date());
                            defectPicture.setPictureAddress(imgPath);
                            defectPicture.setPictureName(fileName);
                            defectPicture.setPictureCode(ele.getRemark());
                            if(ele.getId()!=null && ele.getId()!=""){
                            defectPicture.setId(Long.parseLong(ele.getId()));
                            }
                            defectPicture.setQuestion(question); 
                            defectPicture.setType(1);
                            defectPictureRepository.save(defectPicture); 
                        } 
                        }
                    }
                    }
                     
            }

四、根据对象获取 

    @RequestMapping("/task/createTaskProcess") 
    @ResponseBody
    public JSonObject createTaskProcess(@RequestBody TaskProcess taskProcess 
    ) throws IOException { 
        JSonObject result = new JSonObject();
        String msg = "error";//返回信息
        int code = 1;
        try { 
        taskProcess.setStartTime(new Date());
        maintenanceService.saveTaskProcess(taskProcess);
        msg = "success";
        code = 0;
        } catch (Exception e) {
        e.printStackTrace();
        }
        result.put("code", code);
        result.put("msg", msg);
        result.put("result", "");
        //response.getOutputStream().write(result.toString().getBytes(), 0, result.toString().getBytes().length);
        return result;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644124.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号