• backend-learning: personal blog(1)


    问题记录:

    跨度太大,无法完成,遂决定从基础学起。
    规划路线:
    1.完成JAVA与c++语言差异部分,(注解,其实没多少)
    2.上springboot官网查看开发手册,了解大致原理。
    3. 开始挑选项目上手。
    4.上手过程遇到困难再学别的。
    1.无法连接数据库,springboot报错
    在这里插入图片描述

    hello

    hello函数用@GetMapping来激活

    package com.example.demo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
        @GetMapping("/hello")
        public String hello(@RequestParam(value = "name", defaultValue = "Tomato🍅") String name) {
            return String.format("Hello %s!", name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    效果:
    在这里插入图片描述

    1. 统一结果封装

    后端的请求使用https, 返回形式为
    code msg data
    这里需要将返回的数据用几个方法统一封装起来
    使用方法:serializable.
    有些方法我没有实现,不知道为什么就能用了,如setCode

    • Result.java
      在这里插入图片描述
    • Usercontroller.java
      在这里插入图片描述
      封装:
      在这里插入图片描述

    2.

    3. 跨域问题记录

    跨域的函数得用另一个,但是换回来之后又没问题了,所以两个都是通用的

    /CorsConfig.java
    package com.markerhub.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    /**
     * 解决跨域问题
     */
    @Configuration
    public class CorsConfig implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    ..allowedOrigins("*")//这个需要修改 --》  .allowedOriginPatterns("*")
                    .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                    .allowCredentials(true)
                    .maxAge(3600)
                    .allowedHeaders("*");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述


    在这里插入图片描述


    前端测试数据拉取:
    在这里插入图片描述

  • 相关阅读:
    VPS是干嘛用的?有哪些知名牌子?与云服务器有什么区别?
    Java复习五:抽象类+模板设计方法+接口+三种工厂模式
    手把手教你安装 Linux Mint系统
    震精!京东T8大牛每天熬夜到凌晨三四点,竟然是在写Docker教程
    Vue-router
    强缓存与协商缓存
    开发知识点-uniapp微信小程序-开发指南
    免编程经验,搭建宠物店小程序轻松实现
    RTL8715AH,RTL8715AQ双频WI-FI物联网低功耗SOC
    数据结构笔记 4 树和二叉树
  • 原文地址:https://blog.csdn.net/qq_19875729/article/details/134059766