Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Rest API - get empty brackets instead of json when sending GET request

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rest API - get empty brackets instead of json when sending GET request

    Hello. I really need help. I've been trying to solve this for days now. I am quite a newbie and am going crazy.

    I have a maven project. I can succesfully bring out data from my database into the console in my application server. However, when I try to use a restful services and use Postman to get the data out it only returns an empty pair of brackets [] instead of the data in json-format! I've tried various ways to solve it but this I won't get pass this... I have tried to convert it to Json using GSON before I request it but it doesn't work.



    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>

    Thanks in advance...
    Last edited by Helgesson; November 11th, 2019 at 02:45 PM. Reason: Problems attaching the code

Similar Threads

  1. consume the rest service by passing the json object
    By StarRocks in forum Object Oriented Programming
    Replies: 0
    Last Post: January 29th, 2013, 06:28 AM
  2. [Request]Explanation about sending and recieving data
    By 123099 in forum Java Networking
    Replies: 6
    Last Post: October 6th, 2011, 12:41 PM
  3. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM
  4. Problem sending POST request with Java..
    By lost in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 09:16 PM