backend-learning: personal blog(1)

问题记录:

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

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);
    }
}

效果:
backend-learning: personal blog(1)_第2张图片

1. 统一结果封装

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

  • Result.java
    backend-learning: personal blog(1)_第3张图片
  • Usercontroller.java
    backend-learning: personal blog(1)_第4张图片
    封装:
    backend-learning: personal blog(1)_第5张图片

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("*");
    }
}


backend-learning: personal blog(1)_第6张图片


backend-learning: personal blog(1)_第7张图片


前端测试数据拉取:
backend-learning: personal blog(1)_第8张图片

你可能感兴趣的:(后端)