RESTful webservice:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getJson() {
List<ResultLadok> results = DriverDBLadok_testing.getLadokResultsArrayList();
return Response.ok(results)
.build();
}
package controller_ladok;
public class DriverDBLadok_testing {
public static List<ResultLadok> resultList = new ArrayList();
static String jsonString;
public DriverDBLadok_testing() {
}
public List<ResultLadok> getResultList() {
return resultList;
}
public void setResultList(List<ResultLadok> resultList) {
this.resultList = resultList;
}
//method to put result in List
public static void addResult(String betyg, int poang, int provNr, String termin,
String datum, String kurskod, String kurstillfalle){
ResultLadok resLadok = new ResultLadok();
resLadok.setBetyg(betyg);
resLadok.setPoäng(poang);
resLadok.setProvNr(provNr);
resLadok.setTermin(termin);
resLadok.setDate(datum);
resLadok.setKurskod(kurskod);
resLadok.setKurstillfälle(kurstillfalle);
resultList.add(resLadok);
}
//method to get DB data to arrayList
public static List<ResultLadok> getLadokResultsArrayList(){
try{
//1. get a connection to database
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ladoksimulation", "root", "admin");
//2. create a statement
Statement myStmt = myConn.createStatement();
//3. Execute SQL query
ResultSet myRs = myStmt.executeQuery("select * from resultat");
//4. process the result set
while(myRs.next()){
addResult(myRs.getString("betyg"), myRs.getInt("poäng"), myRs.getInt("provNr"),
myRs.getString("termin"), myRs.getString("date"), myRs.getString("kurskod"),
myRs.getString("kurstillfalle"));
}
}
catch(Exception exc){
exc.printStackTrace();
}
return resultList;
}
}
Pom file:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>ladokRestApi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ladokRestApi-1.0-SNAPSHOT</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<jakartaee>8.0</jakartaee>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ladokSimulation</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${jakartaee}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${jakartaee}</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>