0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

Let`s have a example from your code:

You try to make a request to localhost:8080/users/create in postmap with POST method, but your code show another thing. Based on your controller to make a call to

@PostMapping("/create")
public void create(@RequestBody Tickets tickets)

You should invoke next one URL:

localhost:8080/tickets/create with a request body of Tickets class.

Of course your requestMapping on controller class should be @RequestMapping("/tickets") instead of @RequestMapping("tickets")

UPDATE

JavaDocs for @RequestMapping()

enter image description here

Andrei Lisa
  • 1,361
  • 3
  • 11
  • That slash before name of mapping is not necessary. You should correct that one – Morph21 Aug 08 '23 at 07:40
  • @Morph21 i just updated my answer based on javadocs, i gave a answer to question based on docs – Andrei Lisa Aug 08 '23 at 08:43
  • Well, you said that it should be with slash, which is not true. You can read more about it here https://stackoverflow.com/questions/12742695/use-or-not-leading-slash-in-value-for-requestmapping-need-official-docs-or-poi – Morph21 Aug 08 '23 at 09:04
  • sir , there are said that **It does not matter: If the path does not start with an / then Spring (DefaultAnnotationHandlerMapping) will add it.** – Andrei Lisa Aug 08 '23 at 09:10
  • and i wrote, that it should be, it means that is not a required or a must, but an advice. – Andrei Lisa Aug 08 '23 at 09:13