I want to create a website/API, that reads in a csv and returns the wanted resoureces. I use SpringBoot: the Spring Web dependency. For reading in the csv I import implementation('com.opencsv:opencsv:5.6')
to dependencies
in my build.gradle
-file. I decided to use the following structure:
Four java-files in src\main\java\com\example\so
:
The bean Car.java
:
package com.example.so;
import com.opencsv.bean.CsvBindByName;
import java.math.BigDecimal;
public class Car {
@CsvBindByName
private int id;
@CsvBindByName(column = "name")
private String brand;
@CsvBindByName
private BigDecimal price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"id=" + id +
", brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
To display the correct car I use CarController.java
:
package com.example.so;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CarController {
@GetMapping("/car/{id}")
public Car findcar(@PathVariable int id) {
for (Car car: ReadInCSV.cars){
if (car.getId()==id){
System.out.println(car.toString());
return car;
}
}
return null;
}
@GetMapping("")
public String findcar() {
return "Hi!";
}
}
To read in the csv-file ReadInCSV.java
:
package com.example.so;
import com.opencsv.bean.CsvToBeanBuilder;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
public class ReadInCSV {
static final String fileName="src/main/resources/static/abc.csv";
static List<Car> cars;
static {
try {
cars= new CsvToBeanBuilder(new FileReader(fileName)).withType(Car.class).build().parse();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
and to start the webservice SoApplication.java
:
package com.example.so;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SoApplication {
public static void main(String[] args) {
SpringApplication.run(SoApplication.class, args);
}
}
The file with the data abc.csv
:
id,name,price
1,Audi,52642
2,Mercedes,57127
3,Skoda,9000
4,Volvo,29000
5,Bentley,350000
6,Citroën,21000
7,Füll,41400
8,Rosé,21600
9,Toyota,26700
It works more or less fine, but when you enter http://localhost:8080/car/6
, my browser (Firefox) displays "Citroën" instead of "Citroën". Reading in the csv seems to work fine, because when you print the bean using its .toString()
you get Car{id=6, brand='Citroën', price=21000}
. So apparently the json-conversion is the problem. What can I do to solve this issue?
I am new to world of java-web, so feel free telling me if there are some other problems with my approach.