postman SpringBoot API的问题,未被检测到

kpbwa7wx  于 2023-10-18  发布在  Postman
关注(0)|答案(1)|浏览(145)

我有这个学校的项目,但API没有得到检测的html或 Postman ,在 Postman 它发送一个404错误。
当我运行它时,API不会显示错误,但当我尝试使用Postman时,它不会检测到错误。
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;
    }
}

门票申请:

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("*");
    }
    };
    }
}

有人能帮帮我吗?
我的API工作和连接。

t3psigkw

t3psigkw1#

让我们从你的代码中举一个例子:
你尝试在postmap中使用POST方法向localhost:8080/users/create发出请求,但你的代码显示了另一件事。基于您的控制器调用

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

你应该调用下一个URL:
localhost:8080/tickets/create,请求体为Tickets类。
当然你的requestMapping在控制器类上应该是@RequestMapping("/tickets")而不是@RequestMapping("tickets")

更新

@RequestMapping()的JavaBean

相关问题