I have this school project, but the API is not getting detected by the html or Postman, in Postman it sends a 404 error.
The API when I run it doesn't show errors, but when I try to use Postman doesn't detect it.
TicketsRest:
package zm.rest;
import java.util.List;
import java.util.Optional;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zm.dao.TicketDAO;
import zm.model.Tickets;
@RestController
@RequestMapping("tickets")
public class TicketsRest {
@Autowired
private TicketDAO ticketDAO;
/*guardar*/
@PostMapping("/create") //localhost:8080/users/create
public void create(@RequestBody Tickets tickets) {
/*int id = userDAO.findAll().get(userDAO.findAll().size()-1).getId()+1;*/
int id = 0;
if(ticketDAO.findAll().size() == 0) {
id = 1;
}
else {
id = ticketDAO.findAll().get(ticketDAO.findAll().size()-1).getId()+1;
}
tickets.setId(id);
ticketDAO.save(tickets);
}
@GetMapping("/list") //localhost:8080/users/create
public List<Tickets>list(){
return ticketDAO.findAll();
}
@PutMapping("/update/{id}")
public void update(@PathVariable int id, @RequestBody Tickets updateTickets) {
Optional<Tickets> ticketsOptional = ticketDAO.findById(id);
Tickets tickets = ticketsOptional.orElse(null);
if(tickets != null)
{
tickets.setMotive(updateTickets.getMotive());
tickets.setDepartment(updateTickets.getDepartment());
tickets.setPriority(updateTickets.getPriority());
tickets.setDepartment(updateTickets.getDepartment());
tickets.setDescription(updateTickets.getDescription());
ticketDAO.save(tickets);
}
}
@DeleteMapping("/delete/{id}")
public boolean delete(@PathVariable int id) {
Optional<Tickets> ticketsOptional = ticketDAO.findById(id);
Tickets tickets = ticketsOptional.orElse(null);
if(tickets != null)
{
ticketDAO.delete(tickets);
return true;
}
return false;
}
}
TicketDAO:
package zm.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import zm.model.Tickets;
public interface TicketDAO extends JpaRepository<Tickets, Integer> {
}
Tickets.java:
package zm.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Tickets {
@Id
private Integer id;
@Column
private String motive;
@Column
private String priority;
@Column
private String department;
@Column
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMotive() {
return motive;
}
public void setMotive(String motive) {
this.motive = motive;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
}
TicketsApplication:
package zm.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
public class TicketsApplication {
public static void main(String[] args) {
SpringApplication.run(TicketsApplication.class, args);
}
@Bean
WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping( "/**").allowedOrigins( "http://localhost:4200").allowedMethods("*").allowedHeaders("*");
}
};
}
}
Can someone help me?
My API to work and connect.