英文:
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实体编码的问题,例如 "
应该被替换为双引号 "
,以便代码能够正确运行。
英文:
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("/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"
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论