在STS中启动时,我的spring boot应用程序运行正常,但是如果我尝试用jar,这种方法会杀死它吗?

laklaklak:

在Spring Tool Suite中,该应用程序运行正常。

当我在浏览器中运行应用程序时,得到以下响应:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Sep 06 20:46:04 CEST 2019
There was an unexpected error (type=Internal Server Error, status=500).
No message available

本地主机:8080,索引页完全正常。如果我点击了调用“ / getinfo”页面的按钮,则使“ getTrades()”运行的原因是出现了whitelabel错误。

我发现它死于“ public List getItemNamesAndIDsFromTXT()”函数中的以下行:

String content = "";
        try {
            content = new String(Files.readAllBytes(file.toPath()));
        } catch (IOException e) {
            System.out.println("GetInfoService.java --> getItemNamesAndIDsFromTXT() --> content = new String(Files.readAllBytes(file.toPath())); --> " + e);
        }

这是我的(唯一)控制器:

@Controller
public class IndexController {

    @Autowired
    GetInfoService gis = new GetInfoService();

    @Autowired
    CalculateProfitService cp = new CalculateProfitService();


    @GetMapping("/")
    public String getIndex(Model model) {

        return "index";
    }


    @RequestMapping("/getinfo") 
    public String gotTheData(
            @RequestParam(name="fromCity") String fromCity, 
            @RequestParam(name="toCity") String toCity, 
            @RequestParam("profitMinimum") int profitMinimum, 
            @RequestParam("profitMaximum") int profitMaximum, 
            @RequestParam("auctionTax") int auctionTax, 
            Model model) {  


        model.addAttribute("trades", gis.getTrades(fromCity, toCity, profitMinimum, profitMaximum, auctionTax));    

        return "index";
    }
}

这是我的服务:

@Service
public class GetInfoService {

    public List<Item> getItemNamesAndIDsFromTXT() {

        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:ids");
        } catch (FileNotFoundException e) {
            System.out.println("GetInfoService.java --> getItemNamesAndIDsFromTXT() --> file = ResourceUtils.getFile(\"classpath:ids\"); --> " + e);
        }
        String content = "";
        try {
            content = new String(Files.readAllBytes(file.toPath()));
        } catch (IOException e) {
            System.out.println("GetInfoService.java --> getItemNamesAndIDsFromTXT() --> content = new String(Files.readAllBytes(file.toPath())); --> " + e);
        }
        List<String> lines = Arrays.asList(content.split("\\n"));

        List<Item> items = new ArrayList<>();

        for (String i : lines) {
            String uniqueName = i.substring(i.indexOf(':') + 1, i.lastIndexOf(':')).trim();
            String tier = uniqueName.substring(0, 2);

            String itemName = i.substring(i.lastIndexOf(':') + 2).trim();
            if (uniqueName.contains("@")) {
                itemName = itemName + uniqueName.substring(uniqueName.indexOf('@'));
            }
            items.add(new Item(uniqueName, itemName, tier));

        }

        return items;
    }

    public ArrayList<Offer> getOffers(String fromCity, String toCity, int auctionTax) {
        final List<Item> items = getItemNamesAndIDsFromTXT();
        ArrayList<Offer> offers = new ArrayList<>();

        String firstPartOfUrl = "https://www.albion-online-data.com/api/v2/stats/prices/";
        String secondPartOfUrl = "";
        String thirdPartOfUrl = "?locations=";
        String fourthPartOfUrl = "";
        String fifthPartOfUrl = "&qualities=0";

        fourthPartOfUrl = fromCity + "," + toCity;
        //System.out.println(fourthPartOfUrl);

        String url = "";

        int n = 1;

        JSONFromURL jfu = new JSONFromURL();

        for (Item i : items) {
            secondPartOfUrl += i.getId() + ",";
            n++;

            if (n % 19 == 0 || (i.equals(items.get(items.size() - 1)))) {
                secondPartOfUrl = secondPartOfUrl.substring(0, secondPartOfUrl.length() - 1);
                url = firstPartOfUrl + secondPartOfUrl + thirdPartOfUrl + fourthPartOfUrl + fifthPartOfUrl;
                // System.out.println(url);
                JSONArray jsonarray = jfu.callMeOnArray(url);

                for (int j = 0; j < jsonarray.length(); j++) {
                    JSONObject obj = jsonarray.getJSONObject(j);
                    String itemID = obj.getString("item_id");

                    Item tempItem = items.stream().filter(e -> itemID.equals(e.getId())).findAny().orElse(null);

                    if (tempItem != null && obj.getInt("sell_price_min") > 0) {
                        String city = obj.getString("city");
                        int sellPriceMin = obj.getInt("sell_price_min");
                        if (toCity.contains(city)) sellPriceMin = (int) (sellPriceMin * (100 - auctionTax) / 100);
                        String sellPriceMinDate = obj.getString("sell_price_min_date");
                        int buyPriceMax = obj.getInt("buy_price_max");
                        String buyPriceMaxDate = obj.getString("buy_price_max_date");

                        offers.add(new Offer(tempItem, city, sellPriceMin, sellPriceMinDate, buyPriceMax,
                                buyPriceMaxDate));
                    }
                }

                secondPartOfUrl = "";

            }
        }

        // offers.stream().forEach(e -> System.out.println(e));

        return offers;

    }

    public List<Trade> getTrades(String fromCity, String toCity, int profitMinimum, int profitMaximum, int auctionTax)  {

     long start = System.currentTimeMillis();
        List<Trade> trades = new ArrayList<>();

        ArrayList<Offer> offers = new ArrayList<>();
        try {
            offers = getOffers(fromCity, toCity, auctionTax);
        } catch (JSONException e) {
            System.out.println("GetInfoService.java --> getTrades(String fromCity, String toCity, String profitInput, String profitMaximum) --> " + e);
        }

        if (fromCity.contains("FortSterling")) fromCity = fromCity.substring(0, fromCity.indexOf("Sterling")) + " " + fromCity.substring(fromCity.indexOf("Sterling"));
        if (  toCity.contains("FortSterling")) toCity   = toCity  .substring(0,   toCity.indexOf("Sterling")) + " " +   toCity.substring(toCity  .indexOf("Sterling"));


        final String fromCityFinal = fromCity;
        final String toCityFinal = toCity;

        List<Offer> fromOffers = offers.stream()
                .filter(e -> fromCityFinal.contains(e.getCity()))
                .collect(Collectors.toList());
        List<Offer> toOffers = offers.stream()
                .filter(e -> toCityFinal.contains(e.getCity()))
                .collect(Collectors.toList());

        for (int i = 0; i < fromOffers.size(); i++) {
            Offer fromOffer = fromOffers.get(i);

            for (int j = 0; j < toOffers.size(); j++) {
                Offer toOffer = toOffers.get(j);

                if (fromOffer.getItem().getId().equals(toOffer.getItem().getId())) {
                    if ((toOffer.getSellPriceMin() - fromOffer.getSellPriceMin() > profitMinimum) && (toOffer.getSellPriceMin() - fromOffer.getSellPriceMin() <= profitMaximum)) {
                        trades.add(new Trade(fromOffer, toOffer));
                    }
                }
            }

        }

        Collections.sort(trades);

        // trades.stream().forEach(e->System.out.println(e));

         long end = System.currentTimeMillis();

         System.out.println("It took " + (end-start) + " milliseconds!");

        return trades;
    }

这是pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.transportfromcitytocity</groupId>
    <artifactId>transportfromcitytocity</artifactId>
    <version>0.1</version>
    <name>TransportFromCityToCity</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>
    </dependencies>

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

</project>

这是application.properties:

server.port=8080

谢谢

mavriksc:

该文件不存在。字节在jar中,但是您不能从这样的路径加载它。在IDE中运行该文件时,该文件存在该路径。

InputStream is ={CLASSNAMEHERE}.class.getClassLoader().getResourceAsStream(path);

然后您可以获取字节并从此处转换为字符串。

public class ResourceToString {
public static void main(String[] args) {
    String pi  = convertStreamToString(ResourceToString.class.getClassLoader().getResourceAsStream("pi.txt"));
    System.out.println(pi);

}
private static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}
}  

在此示例中,pi.txt在我的资源文件夹中\{projDir}\src\main\resources

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章