Cannot render error page for request [/countries] as the response has already been committed. As a result, the response may have the wrong status code

Viewed 77

I want to read data from services to controler and display on the UI on Spring boot, but it gives the below error

ERROR 20984 --- [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/countries] as the response has already been committed. As a result, the response may have the wrong status code.

The database has also the below data

mysql> select * from country;

+----+-------------+------+---------------+---------------+-------------+
| id | capital     | code | continent     | description   | nationality |
+----+-------------+------+---------------+---------------+-------------+
|  1 | Washington  | 01   | North America | United States | American    |
|  2 | Addis Ababa | 01   | Africa        | Ethiopia      | Ethiopian   |
+----+-------------+------+---------------+---------------+-------------+ 

Model

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Country {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String code;
    private String capital;
    private String description;
    private String nationality;
    private String continent;
    
    @OneToMany(mappedBy="country")
    private List<State> states;
}

Repository

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.kindsonthegenius.FleetApp.models.Country;

@Repository
public interface CountryRepository extends JpaRepository<Country, Integer> {

}

Service

  @Service
public class CountryService {

    @Autowired
    private CountryRepository countryRepository;
    
    public List<Country> getCountries(){
        return countryRepository.findAll();
    }
    
}

Controller

@Controller
public class CountryController {
@Autowired
private CountryService countryService;

@GetMapping("/countries")
public String goCountry(Model model) {
    List<Country> countryList = countryService.getCountries(); 
    model.addAttribute("countries", countryList);
    return "country";
}
}

UI

<table class="table">
            <thead>
              <tr>
                <th scope="col">id</th>
                <th scope="col">Description</th>
                <th scope="col">Capital</th> 
              </tr>
            </thead>
            <tbody> 
              <tr th:each="country : ${countries}">
                <td th:text="${country.id}"></td>
                <td th:text="${country.description}"></td>
                <td th:text="${country.capital}"></td> 
              </tr>
            </tbody>
       </table>

Please help me to fix this error?

1 Answers

I guess this is is happening because of @OneToMany relationship between countries and state. If you share your "State" Model I can tell you what to change. However, this error usually happens when countries are loaded with state and state also has country in it. This creates endless JSON Tree. Check this Link Springboot Hibernate One to Many infinty recursion

Related