springboot项目在idea上本地运行时是没有任何问题的,但当我用idea把项目打包成jar后在服务器上运行jar的时候虽然能正常运行,但是controller接口却报了404错误导致没有办法加载到数据?

springboot项目在idea上本地运行时是没有任何问题的,但当我用idea把项目打包成jar后在服务器上运行jar的时候虽然能正常运行,但是controller接口却报了404错误导致没有办法加载到数据?

问题描述:

问题

如题,springboot项目,在idea运行无问题,如图:

图片说明

此处地址栏为 localhost:8080
图片说明

但当我将项目打包为jar通过java -jar运行的时候,虽然能正常部署但controller接口却报404错误,如图:

图片说明

此处地址栏为 服务器ip:8080

图片说明
图片说明

部分代码

controller

package net.suncaper.epidemicdataanalysis.controller;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/uv")
public class ChinaDayController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("chinaData")
    @ResponseBody
    public List<Map<String, Object>> listUvCovid(){
        //中国每日各省份数据
        String sql = "select * from chn_data_per_province_by_date";
        return jdbcTemplate.queryForList(sql);
    }

    @GetMapping("chinaCure")
    @ResponseBody
    public List<Map<String, Object>> listCure(){
        //中国每日各省份数据
        String sql = "select * from chn_cure_rate_per_province";
        return jdbcTemplate.queryForList(sql);
    }

    @GetMapping("chinaDeath")
    @ResponseBody
    public List<Map<String, Object>> listDeath(){
        //中国每日各省份数据
        String sql = "select * from chn_death_rate_per_province";
        return jdbcTemplate.queryForList(sql);
    }

    @GetMapping("chinaLineChart")
    @ResponseBody
    public List<Map<String, Object>> listLineChart(){
        String sql = "select * from china_line_chart";
        return jdbcTemplate.queryForList(sql);
    }
}

application

package net.suncaper.epidemicdataanalysis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EpidemicDataAnalysisApplication {

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

}

pom

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

404是找不到页面,也就是说跟你前端的资源文件有关 html/js/css这些资源文件

打包的pom.xml的插件有问题,没有把资源文件加载进去

这是你接口没访问到404,不是数据库没连到

如果是前后端不分离的话,应该是你打包的时候前端文件没打包进去。

漂亮,截图完美的避开了404最需要查看的浏览器地址栏