简体   繁体   中英

GET Parameter not coming through REST API using AngularJS

I'm trying to pass a single parameter to the server with a GET request. I can see in the browser that the GET request with the paramter "loaded" is correct and successfully sent. The problem is that it is not successfully converted into an object on the server side. The request fails because the object from the parameter is empty and therefore throwing an exception. The application is a table that I plan to make dynamiclly loadable. When you reach the end of the site the next 25 entries should get loaded with a GET request and the count of how many entries are already loaded as a parameter.

Server side

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

TableUtils tu = new TableUtils();

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Eintrag> fetchEntries(String loaded) {
    List<KeyEntry> entries = tu.getEntries();
    Integer entriesLoaded = Integer.parseInt(loaded);

    if(entries.size() > (entriesLoaded+25)) {
        entries = entries.subList(entriesLoaded, entriesLoaded + 25);
    }
    else if(entries.size() == (entriesLoaded+25)){
        entries.clear();
    }
    else {
        entries = entries.subList(entriesLoaded, entries.size());
    }

    return convert(entries);
}

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String add(final Eintrag input) {
    if(input.getlKey()!= "" && input.getOs() != "" && 
tu.addEntry(input.getOs(), input.getlKey()))
        return "success";
    else
        return "error";
}

private List<Eintrag> convert(List<KeyEntry> entries) {
    List<Eintrag> eintraege = new ArrayList<Eintrag>();
    for (KeyEntry entry : entries) {
        Eintrag eintrag = new Eintrag();
        eintrag.setId(entry.getId());
        eintrag.setOs(entry.getOs());
        eintrag.setlKey(entry.getlKey());
        eintraege.add(eintrag);
    }
    return eintraege;
}
}

Controller

angular.module("keyDB", []).controller("Entries", function($scope, $http){
$http.get("http://localhost:8080/keyDBmaven/rest/hello",{params : {loaded : 
"0"}}).then(function(response){
    $scope.keys = response.data;
});
});

Request Headers

GET /keyDBmaven/rest/hello?loaded=0 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36
Referer: http://localhost:8080/keyDBmaven/index.jsp
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,zh CN;q=0.6,zh;q=0.5,zh-TW;q=0.4
Cookie: JSESSIONID=8B5227C67F86B7B50B8E180066E5B055

I also tried to receive the parameter with an extra java bean as target object:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class bean {
    public String loaded;

    @XmlElement
    public String getLoaded() {
        return loaded;
    }

    public void setLoaded(String loaded) {
    this.loaded = loaded;
    }
}

Which also was always null.

I'm sitting at this problem for an extended amount of time so a solution would be very much appreciated.

Your "loaded" parameter is query parameter and to get it from the request with Jax-Rs implementation you need to use @QueryParam("param_name") annotation. So your call should look like

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Eintrag> fetchEntries(@QueryParam("loaded") String loaded) {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM