基于springboot搭建web项目遇到的问题汇总

1.application类要放在最外层的package下;

2.整合mybatis时,@MapperScan注解要扫描到mapper包,不然调不到

@SpringBootApplication
@MapperScan("com.test.mapper")
public class TestApplication {

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

3.@RestController = @Controller + @ResponseBody:

1)@RestController:方法返回值自动转为json,若想返回模板要使用@Controller

2)@Controller:方法返回模板,若要返回json需在方法上加@ResponseBody

@Controller
public class AnimeController {
    // 返回index模板
    @RequestMapping("/index_1")
    public String index_1() {
        return "index";
    }

    // 返回“index”字符串
    @ResponseBody
    @RequestMapping("/index_2")
    public String index_2() {
        return "index";
    }
}

@RestController
public class AnimeController {
    // 返回“index”字符串
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
}

 4.前端使用vue+elementUI,启动端口8080,后端启动端口8081,前台发送请求后台接收不到,需要前端配置project\config\index.js

    proxyTable: {
      '/api': {
        target: 'http://localhost:8081', //域名
        changeOrigin:true,
        pathRewrite:{
          '^/api':''
        }
      }
    }

发送请求为:/api/xxx

猜你喜欢

转载自blog.csdn.net/qq25616804/article/details/81167290