Nasreddin
Nasreddin

Reputation: 1657

rest api returns empty bracket for GET request

I implemented Rest api with Spring Boot. In my controller class, I have code to handle GET request which will return JSON if record found.

// SeqController.java
@Autowired
private SeqService seqService;
@RequestMapping( 
        value = "/api/seqs/{analysis_id}", 
        method = RequestMethod.GET, 
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<SeqTb>> getSeqByAnalysisId(@PathVariable("analysis_id") String analysis_id) {
    List<SeqTb> seqs = seqService.findByAnalysisId(analysis_id);
    return new ResponseEntity(seqs, HttpStatus.OK);
}

I also create a bean class SeqServiceBean that extends the interface SeqService which in turn calls methods from the following JPA repository for query.

//SeqRepository.java
@Repository
public interface SeqRepository extends JpaRepository<SeqTb, Integer> {

    @Override
    public List<SeqTb> findAll();

    public List<SeqTb> findByAnalysisId(String analysisId);
}

Problem is when I typed the url (http://localhost:8080/api/seqs/fdebfd6e-d046-4192-8b97-ac9f65dc2009) in my browser, it returned nothing but a pair of empty brackets. I just looked in the database and that record is indeed there. What did I do wrong?

Upvotes: 2

Views: 8109

Answers (2)

Rajnish
Rajnish

Reputation: 54

In most of case, database driver jar is not deployed in server. Check deployment assembly of project in eclipse. Also see console message to check if it is showing driver jar not found. If this is case simply deploy this jar in deployment assembly of eclipse.

One thing, if build path has this jdbc driverjar in eclipse, main method will connect to database. But if jar is not deployed jdbc connection will not happen over http request.

Upvotes: 0

mykey
mykey

Reputation: 2283

A bit late to answer this quesiton, but in case anyone else is having this issue. This problem may be caused by the class (that we want to be displayed as a json object) missing getter and/or setter methods. In your case the "seqTab" class may be not have getters. Without the getters our application can not extract the fileds to build the json object.

Example : Sample user class public class User {

private String firstname;
private String lasttname;
int age;

public User(){

}

public User(String fname, String lname, int age){
    this.firstname = fname;
    this.lasttname = lname;
    this.age = age;
}

}

Sample rest controller @RestController

public class SampleRS {

@RequestMapping(value = {"/sample/{input}"}, method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> startService(@PathVariable("input") String input){
     User u = new User(input,"bikila",45);

    return new ResponseEntity<User>(u,HttpStatus.OK);
}

}

// If we try to hit the endpoint /sample{input} .. e.g.

Request : localhost:8080/Sample/abebe

Response :
{}

But adding the getters for the User class will solve the problem.

Modified User class with getters

public class User {

private String firstname;
private String lasttname;
int age;

public User(){

}

public User(String fname, String lname, int age){
    this.firstname = fname;
    this.lasttname = lname;
    this.age = age;
}

public String getFirstname() {
    return firstname;
}

 public String getLasttname() {
    return lasttname;
}

public int getAge() {
    return age;
}

}

Request : http://localhost:8080/sample/abebe

Response : {"firstname":"abebe","lasttname":"bikila","age":45}

Hope that helps !

Upvotes: 6

Related Questions