In the Spring Tool Suite the application is working perfectly.
When I run my application in browser I get this response:
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
localhost:8080, index page perfectly fine. If I hit the button that calls the "/getinfo" page what makes the "getTrades()" to run, I get this whitelabel error.
I found out that it dies at these lines in the "public List getItemNamesAndIDsFromTXT()" function:
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);
}
This is my (only) controller:
@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";
}
}
This is my service:
@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;
}
This is the 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>
This is the application.properties:
server.port=8080
Thanks