qq_44792784
qq_44792784
2020-06-04 15:48

后端服务器Spring boot 启动访问特定接口接口时一直报如下错误:WARN 1952 --- [-nio-443-exec-5] o.s.web.servlet.PageNotFound : No mapping for GET / ?

  • spring
  • java
  • 微信小程序

报错信息

报错信息GET/后面也没有相应路径,当前端调用这个接口时候,接口内所有逻辑功能都能正确完成,但是会报这个错误。

我找了很多博客,试过一些方法,但是都没有解决,这个接口是给微信小程序前端使用的,前端调用接口能正常进行(正常完成了改接口的所有逻辑功能),但是在回调函数中,却进入fail模块而非success模块。上述问题仅在手机真机调试或者小程序手机登录体验版本时存在,用电脑使用开发者工具模拟器时小程序调用该接口则能正确进入sucess回调函数,这是为什么呢?

application中配置了http转向https,代码如下:


    @MapperScan("wx.com.mapper")

    @SpringBootApplication(scanBasePackages = "wx")
    public class WxMiniprogramApplication {

    public static void main(String[] args) {
        SpringApplication.run(WxMiniprogramApplication.class, args);
    }

    //拦截所有请求
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override            
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");  
                SecurityCollection collection = new SecurityCollection();  
                collection.addPattern("/*");                
                constraint.addCollection(collection);                
                context.addConstraint(constraint);            
                }
        };
            tomcat.addAdditionalTomcatConnectors(httpConnector());
            return tomcat;
    }
    @Bean    
    public Connector httpConnector() {        
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");        
        connector.setScheme("http");        //Connector监听的http的端口号        
        connector.setPort(80);              //监听http默认端口号80    
        connector.setSecure(false);          
        connector.setRedirectPort(443);     //监听到http的端口号后转向到的https的端口号
        return connector;    
        }

此外,调用到的后端接口代码如下:

    public Result uploadFile(HttpServletRequest request, HttpServletResponse response,@PathVariable int recid,
            @PathVariable String theme,@PathVariable String extramsg,@PathVariable String openid) throws Exception {




        RecFile recfile  = new RecFile();

        String name = null;

        //获取文件需要上传到的路径

        File directory = new File("C:\\微信小程序上传文件");
        String path = directory.getCanonicalPath() + "\\upload\\";

        // 判断存放上传文件的目录是否存在(不存在则创建)
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        request.setCharacterEncoding("utf-8"); //设置编码
        JSONArray jsonArray = new JSONArray();
        try {

            StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;

            Iterator<String> iterator = req.getFileNames();

            while (iterator.hasNext()) {
                HashMap<String, Object> res = new HashMap<String, Object>();

                MultipartFile file = req.getFile(iterator.next());

                // 获取文件名

                String fileNames = file.getOriginalFilename();


                if(fileNames.substring(0,4).equals("tmp_")) {//此处踩了一个大坑,pc端和手机端发送至后台的文件名格式是不一样的,需要分别进行判断和处理
                    //如果为手机端上传的文件
                    name = fileNames;
                    String destPath = path + name;

                    //真正写到磁盘上

                    File file1 = new File(destPath);

                    OutputStream out = new FileOutputStream(file1);

                    out.write(file.getBytes());

                    res.put("url", destPath);



                    jsonArray.add(res);

                    out.close();


                }
                else {//文件由pc端提交
                    String temp[] = fileNames.split("\\.");




                    //获取文件的唯一文件名
                    String uniqueName = temp[2];

                    //获取上传文件的后缀名
                    String extName = temp[3];

                    //组成新的文件名称

                    String newName = uniqueName + "." + extName;

                    name = newName;



                    String destPath = path + name;

                    //真正写到磁盘上

                    File file1 = new File(destPath);

                    OutputStream out = new FileOutputStream(file1);

                    out.write(file.getBytes());

                    res.put("url", destPath);



                    jsonArray.add(res);



                    out.close();

                }

            }

        } catch (Exception e) {
            Result result = new Result(403,"出现错误:" + e.getMessage(),0,null);
            System.out.println(e.getMessage());
            return result;
        }

        recfile.setValue(jsonArray);











        //此处改用ServletOutputStream避免使用prinWriter时出现的不能设置方法返回结果的问题
        ServletOutputStream output = response.getOutputStream();

        response.setContentType("application/json");

        response.setCharacterEncoding("utf-8");

        output.write(JSON.toJSONString(recfile).getBytes());

        output.flush();

        //获取系统当前时间
        Timestamp sendtime = new Timestamp(System.currentTimeMillis());
        //sendid采用自增型主键,replyid此处不做绑定,因此均置为0
        SendRecord sendrecord = new SendRecord(0,recid,theme,extramsg,openid,name,sendtime,0);
        List<SendRecord> list = new ArrayList<SendRecord>();
        list.add(sendrecord);
        try {
            allinoneserviceimpl.addSend(sendrecord);
        }catch(Exception e) {
            Result result = new Result(403,"创建发送记录出现异常" + e.getMessage(),0,null);
            System.out.println(e.getMessage());
            return result;
        }
        Result result = new Result(200,"创建发送记录成功",list.size(),list);
        return result;

    }

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答