Đạt Phạm
Đạt Phạm

Reputation: 1

Get method not displaying the data on browser and postman

I was trying to create API with Java. While using the get method I'm not receiving the JSON data. It's only displaying closed braces as response.

I'm attaching the code and output below. Any suggestions might be helpful.

Controller

@GetMapping("/products")
    public ResponseEntity<List<Product>> getProducts() {
        return getProductService.execute(null);
    }

GetProductService Class

public class GetProductService implements Query<Void, List<Product>> {

    public GetProductService(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    private final ProductRepository productRepository;


    @Override
    public ResponseEntity<List<Product>> execute(Void input) {
        List<Product> products = productRepository.findAll();
        return ResponseEntity.status(HttpStatus.OK).body(products);
    }
}

Query Interface

public interface Query <I, O> {
    ResponseEntity<O> execute(I input);
}

ProductRepository Intergface

public interface ProductRepository extends JpaRepository<Product, Integer> {
}

I have tried to print the data on the terminal and it worked but still don't know why it didnt's show on the browser and postman.

Upvotes: 0

Views: 49

Answers (1)

Saeid Kazemi
Saeid Kazemi

Reputation: 1

I hope you are doing well , it is more likely your response is too large

for simulation purposes i have built a large JSON with random values bigger than postman

Max response size

and after calling the API there was nothing as a response , and browser can not handle it too.

Some possible solutions :

1-Paginate the Response to break it down smaller pieces , for example :

@GetMapping("/data")
public ResponseEntity<List<Data>> getData(@RequestParam int page, @RequestParam int size) {
    Pageable pageable = PageRequest.of(page, size);
    Page<Data> dataPage = dataRepository.findAll(pageable);
    return ResponseEntity.ok(dataPage.getContent());
}

2-Truncate the response as below

@GetMapping("/data")
public ResponseEntity<?> getData() {
    List<Data> data = fetchData(); // Your method to fetch data
    if (data.size() > MAX_SIZE) {
        return ResponseEntity.ok(data.subList(0, MAX_SIZE)); // Return truncated data
    }
    return ResponseEntity.ok(data);
}

3-Maximize the response size of postman

Upvotes: 0

Related Questions