I try to access a route from my controller and it returns a 404 error instead of returning the entity in a JSON with POSTMAN

huangapple go评论62阅读模式
英文:

I try to access a route from my controller and it returns a 404 error instead of returning the entity in a JSON with POSTMAN

问题

以下是您要翻译的部分:

"I try to access a route from my controller and it returns a 404 error instead of returning the entity in a JSON with POSTMAN"

"This is my controller"

package com.main.CitaMedica.Controller;

import com.main.CitaMedica.DTO.CitaDTO;
import com.main.CitaMedica.DTO.MedicoDTO;
import com.main.CitaMedica.DTO.PacienteDTO;
import com.main.CitaMedica.Service.CitaService;
import com.main.CitaMedica.Service.MedicoService;
import com.main.CitaMedica.Service.PacienteService;
import org.springframework.beans.factory @Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/citas")
public class CitaController {
    @Autowired CitaService citaService;
    @Autowired MedicoService medicoService;
    @Autowired PacienteService pacienteService;

    @GetMapping("mostrarTodos")
    public ResponseEntity<CitaDTO> showCitas(){
        List<CitaDTO> citas = citaService.findAll();
        return new ResponseEntity(citas, HttpStatus.OK);
    }

    @GetMapping("mostrarUno/{citaID}")
    public ResponseEntity<CitaDTO> showCita(@PathVariable("citaID") int citaID){
        CitaDTO cita = citaService.findById(citaID);
        return new ResponseEntity(cita, HttpStatus.OK);
    }


    /*
    @GetMapping("mostrar/porFecha/{fecha}")
    public ResponseEntity<Cita> showCitaFechaHora(@PathVariable("fecha") Date fecha){
        List<Cita> cita = citaService.findByFechaHora(fecha);
        return new ResponseEntity(cita, HttpStatus.OK);
    }
     */

    @PostMapping("create")
    public ResponseEntity<CitaDTO> crearCita(){
        MedicoDTO medico = medicoService.findById(5);
        PacienteDTO paciente = pacienteService.findById(3);
        CitaDTO cita = new CitaDTO();
        cita.setMedico(medico);
        cita.setPaciente(paciente);
        cita.setFechaHora(new Date());
        cita.setMotivoCita("Prueba");
        citaService.save(cita);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping("update/{citaID}")
    public ResponseEntity<CitaDTO> actualizarCita(@PathVariable("citaID") int citaID){
        CitaDTO cita = citaService.findById(citaID);
        cita.setMotivoCita("Enfermedad");
        citaService.save(cita);
        return new ResponseEntity(HttpStatus.OK);
    }

    @DeleteMapping("delete/{citaID}")
    public ResponseEntity<CitaDTO> delete(@PathVariable("citaID") int citaID){
        citaService.delete(citaID);
        return new ResponseEntity(HttpStatus.OK);
    }
}

"And this is the error that POSTMAN gives me when I try to access that route and I have the server started by port 8090"

{
    "timestamp": "2023-02-06T17:00:50.592+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/citas/mostrarTodos"
}

请注意,您的控制器代码中存在HTML实体编码的问题,例如 &quot; 应该被替换为双引号 ",以便代码能够正确运行。

英文:

I try to access a route from my controller and it returns a 404 error instead of returning the entity in a JSON with POSTMAN

This is my controller

package com.main.CitaMedica.Controller;
import com.main.CitaMedica.DTO.CitaDTO;
import com.main.CitaMedica.DTO.MedicoDTO;
import com.main.CitaMedica.DTO.PacienteDTO;
import com.main.CitaMedica.Service.CitaService;
import com.main.CitaMedica.Service.MedicoService;
import com.main.CitaMedica.Service.PacienteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping(&quot;/citas&quot;)
public class CitaController {
@Autowired CitaService citaService;
@Autowired MedicoService medicoService;
@Autowired PacienteService pacienteService;
@GetMapping(&quot;mostrarTodos&quot;)
public ResponseEntity&lt;CitaDTO&gt; showCitas(){
List&lt;CitaDTO&gt; citas = citaService.findAll();
return new ResponseEntity(citas, HttpStatus.OK);
}
@GetMapping(&quot;mostrarUno/{citaID}&quot;)
public ResponseEntity&lt;CitaDTO&gt; showCita(@PathVariable(&quot;citaID&quot;) int citaID){
CitaDTO cita = citaService.findById(citaID);
return new ResponseEntity(cita,HttpStatus.OK);
}
/*
@GetMapping(&quot;mostrar/porFecha/{fecha}&quot;)
public ResponseEntity&lt;Cita&gt; showCitaFechaHora(@PathVariable(&quot;fecha&quot;)Date fecha){
List&lt;Cita&gt; cita = citaService.findByFechaHora(fecha);
return new ResponseEntity(cita,HttpStatus.OK);
}
*/
@PostMapping(&quot;create&quot;)
public ResponseEntity&lt;CitaDTO&gt; crearCita(){
MedicoDTO medico = medicoService.findById(5);
PacienteDTO paciente = pacienteService.findById(3);
CitaDTO cita = new CitaDTO();
cita.setMedico(medico);
cita.setPaciente(paciente);
cita.setFechaHora(new Date());
cita.setMotivoCita(&quot;Prueba&quot;);
citaService.save(cita);
return new ResponseEntity(HttpStatus.OK);
}
@PutMapping(&quot;update/{citaID}&quot;)
public ResponseEntity&lt;CitaDTO&gt; actualizarCita(@PathVariable(&quot;citaID&quot;) int citaID){
CitaDTO cita = citaService.findById(citaID);
cita.setMotivoCita(&quot;Enfermedad&quot;);
citaService.save(cita);
return new ResponseEntity(HttpStatus.OK);
}
@DeleteMapping(&quot;delete/{citaID}&quot;)
public ResponseEntity&lt;CitaDTO&gt; delete(@PathVariable(&quot;citaID&quot;) int citaID){
citaService.delete(citaID);
return new ResponseEntity(HttpStatus.OK);
}
}

And this is the error that POSTMAN gives me when I try to access that route and I have the server started by port 8090

{
&quot;timestamp&quot;: &quot;2023-02-06T17:00:50.592+00:00&quot;,
&quot;status&quot;: 404,
&quot;error&quot;: &quot;Not Found&quot;,
&quot;message&quot;: &quot;No message available&quot;,
&quot;path&quot;: &quot;/citas/mostrarTodos&quot;
}

Before it worked for me and it returned all the data when I did not have DTO and MapperStructure but since I have implemented it now in all the routes it gives me that

Ask me for any information

答案1

得分: 0

通过在所有的映射器中添加 @Mapper(componentModel="spring"),我解决了我的问题,这是我的错误。

英文:

I solved my problem by adding @Mapper(componentModel="spring") in all mappers it was my mistake

huangapple
  • 本文由 发表于 2023年2月7日 01:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定