Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3643

My spring app is running successfully but I cant open my API in browser?

$
0
0

When I enter localhost:8080/api and try to access different http request I failed.

here is an example of my service class:

package services;import models.Conducteur;import models.Trip;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import repositories.ConducteurRepository;import repositories.TripRepository;import java.time.LocalDate;import java.util.ArrayList;import java.util.List;import java.util.Optional;@Servicepublic class ConducteurService {    @Autowired    private ConducteurRepository conducteurRepository;    @Autowired    private TripRepository tripRepository;    // Méthode pour retourner la liste des conducteurs dispo dans une date spécéfique    public List<Conducteur> findAvailableDrivers(LocalDate date) {        List<Conducteur> allDrivers = (List<Conducteur>) conducteurRepository.findAll();        List<Conducteur> availableDrivers = new ArrayList<>();        for (Conducteur conducteur : allDrivers) {            if (isDriverAvailable(conducteur, date)) {                availableDrivers.add(conducteur);            }        }        return availableDrivers;    }    // Méthode pour vérifier la disponibilité d'un conducteur à une date spécifique    private boolean isDriverAvailable(Conducteur conducteur, LocalDate date) {        // Récupérer tous les voyages pour la date de départ spécifiée        List<Trip> tripsForDate = tripRepository.findAllByDepartureDate(date);        // Vérifier si le conducteur a des voyages pour cette date        for (Trip trip : tripsForDate) {            if (trip.getConducteur().equals(conducteur)) {                return false; // Le conducteur a un voyage prévu pour cette date            }        }        return true; // Le conducteur est disponible pour cette date    }    public long countConducteurs() {        try {            return conducteurRepository.count();        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public void deleteConducteur(Conducteur conducteur) {        try {            conducteurRepository.delete(conducteur);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public void deleteAllConducteurs() {        try {            conducteurRepository.deleteAll();        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public void deleteConducteursByMat(Iterable<String> mat) {        try {            conducteurRepository.deleteAllById(mat);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public boolean existsConducteurByMat(String mat) {        try {            return conducteurRepository.existsById(mat);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public List<Conducteur> findAllConducteurs() {        try {            return (List<Conducteur>) conducteurRepository.findAll();        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public List<Conducteur> findAllConducteursByMat(Iterable<String> mats) {        try {            return (List<Conducteur>) conducteurRepository.findAllById(mats);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public Optional<Conducteur> findConducteurByMat(String mat) {        try {            return conducteurRepository.findById(mat);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public Conducteur saveConducteur(Conducteur conducteur) {        try {            return conducteurRepository.save(conducteur);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }    public List<Conducteur> saveAllConducteurs(Iterable<Conducteur> conducteurs) {        try {            return (List<Conducteur>) conducteurRepository.saveAll(conducteurs);        } catch (Exception e) {            e.printStackTrace();            throw e; // Re-lancer l'exception ou convertir en une exception personnalisée si nécessaire        }    }}

and controllerClass:

package controllers;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.*;import services.ConducteurService;import models.Conducteur;import java.util.List;@RestController@RequestMapping("/api/conducteurs")public class ConducteurController {    @Autowired    private ConducteurService conducteurService;    @GetMapping("")    public ResponseEntity<List<Conducteur>> getAllConducteurs() {        List<Conducteur> conducteurs = conducteurService.findAllConducteurs();        return ResponseEntity.ok(conducteurs);    }    @GetMapping("/{matricule}")    public ResponseEntity<Conducteur> getConducteurByMatricule(@PathVariable String matricule) {        Conducteur conducteur = conducteurService.findConducteurByMat(matricule)                .orElse(null);        if (conducteur != null) {            return ResponseEntity.ok(conducteur);        } else {            return ResponseEntity.notFound().build();        }    }    @PostMapping("/")    public ResponseEntity<Conducteur> createConducteur(@RequestBody Conducteur conducteur) {        try {            Conducteur savedConducteur = conducteurService.saveConducteur(conducteur);            return ResponseEntity.status(HttpStatus.CREATED).body(savedConducteur);        } catch (Exception e) {            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();        }    }    @PutMapping("/{matricule}")    public ResponseEntity<Conducteur> updateConducteur(@PathVariable String matricule, @RequestBody Conducteur conducteurDetails) {        Conducteur conducteur = conducteurService.findConducteurByMat(matricule)                .orElse(null);        if (conducteur != null) {            // Update conducteur details            conducteur.setNom(conducteurDetails.getNom());            conducteur.setPrenom(conducteurDetails.getPrenom());            conducteur.setDate_naissance(conducteurDetails.getDate_naissance());            conducteur.setCIN(conducteurDetails.getCIN());            conducteur.setTrips(conducteurDetails.getTrips());            conducteur.setPermis(conducteurDetails.getPermis());            // Save updated conducteur            Conducteur updatedConducteur = conducteurService.saveConducteur(conducteur);            return ResponseEntity.ok(updatedConducteur);        } else {            return ResponseEntity.notFound().build();        }    }    @DeleteMapping("/{matricule}")    public ResponseEntity<Void> deleteConducteur(@PathVariable String matricule) {        Conducteur conducteur = conducteurService.findConducteurByMat(matricule)                .orElse(null);        if (conducteur != null) {            conducteurService.deleteConducteur(conducteur);            return ResponseEntity.ok().build();        } else {            return ResponseEntity.notFound().build();        }    }}

I tried this :http://localhost:8080/api/conducteurs/

I got this :Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri May 10 15:38:58 WEST 2024There was an unexpected error (type=Not Found, status=404).


Viewing all articles
Browse latest Browse all 3643

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>