gpt4 book ai didi

java - 无法访问 Spring Boot 的请求映射

转载 作者:行者123 更新时间:2023-11-30 06:08:26 26 4
gpt4 key购买 nike

这是我想要做的,使用 RestControllers 从 SQL DB 访问数据,代码编译并运行良好(Tomcat 在端口上启动:8081 (http),上下文路径为“”),但每当我尝试从 http://localhost:8081/getAllStudents 上的 postman 访问映射时,我收到错误{“时间戳”:“2018-06-09T11:03:59.136+0000”,“状态”:404,“错误”:“未找到”,“消息”:“没有可用消息”,“路径”:“/获取所有学生”}

/**Spring boot app**/

package com.akshay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan
@EnableJpaRepositories
@EnableAutoConfiguration
public class StartLearnApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(StartLearnApplication.class, args);
}
}


/**CONTROLLER**/
package com.akshay.spring.controllers;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.akshay.spring.dtos.StudentDTO;
import com.akshay.spring.services.StudentService;

@RestController
@RequestMapping("/api")
public class StudentController {

@Autowired
private StudentService studentService;

@GetMapping("/getAllStudents")
public List<StudentDTO> getAllStudents() {
return studentService.getAllStudents();
}

@GetMapping("/getStudentByRollNumber")
public StudentDTO getStudentBy(@RequestParam(name="rollNumber") String rollNumber) {
return studentService.getStudentBy(rollNumber);
}

@PostMapping("/saveStudent")
public StudentDTO saveStudent(@RequestBody StudentDTO studentDTO) {
return studentService.saveStudent(studentDTO);
}
@PostMapping("/deleteStudentByRollNumber")
public void deleteStudentBy(@RequestParam(name="rollNumber")String rollNumber) {
studentService.deleteStudentBy(rollNumber);
}

}

/**Service Interface**/

package com.akshay.spring.services;

import java.util.List;

import com.akshay.spring.dtos.StudentDTO;

public interface StudentService {

List<StudentDTO> getAllStudents();

StudentDTO getStudentBy(String rollNumber);

StudentDTO saveStudent(StudentDTO studentDTO);

void deleteStudentBy(String rollNumber);

}

/**Service Impl**/

package com.akshay.spring.serviceImpls;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.akshay.spring.dtos.StudentDTO;
import com.akshay.spring.models.StudentModel;
import com.akshay.spring.repositories.StudentRepository;
import com.akshay.spring.services.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

@Autowired
StudentRepository studentRepository;

@Override
public List<StudentDTO> getAllStudents() {
List <StudentModel> studentsModelList = studentRepository.findAll();
List <StudentDTO> studentDTOList = null;
for(StudentModel student: studentsModelList) {
studentDTOList.add(convertModelToDTO(student));
}
return studentDTOList;
}


@Override
public StudentDTO getStudentBy(String rollNumber) {
return convertModelToDTO(studentRepository.getStudentByRollNumber(rollNumber));
}

@Override
public StudentDTO saveStudent(StudentDTO studentDTO) {
studentRepository.save(convertDTOToModel(studentDTO));
return studentDTO;
}

@Override
public void deleteStudentBy(String rollNumber) {
studentRepository.deleteStudentByRollNumber(rollNumber);
}


private StudentDTO convertModelToDTO(StudentModel student) {
StudentDTO studentDTO = new StudentDTO();
studentDTO.setAddress(student.getAddress());
studentDTO.setEmailId(student.getEmailId());
studentDTO.setFathersName(student.getFathersName());
studentDTO.setFirstName(student.getFirstName());
studentDTO.setId(student.getId());
studentDTO.setLastName(student.getLastName());
studentDTO.setMothersName(student.getMothersName());
studentDTO.setPhoneNumber(student.getPhoneNumber());
studentDTO.setRollNumber(student.getRollNumber());
studentDTO.setStandard(student.getStandard());
return studentDTO;
}

private StudentModel convertDTOToModel(StudentDTO student) {
StudentModel studentModel = new StudentModel();
studentModel.setAddress(student.getAddress());
studentModel.setEmailId(student.getEmailId());
studentModel.setFathersName(student.getFathersName());
studentModel.setFirstName(student.getFirstName());
studentModel.setId(student.getId());
studentModel.setLastName(student.getLastName());
studentModel.setMothersName(student.getMothersName());
studentModel.setPhoneNumber(student.getPhoneNumber());
studentModel.setRollNumber(student.getRollNumber());
studentModel.setStandard(student.getStandard());
return studentModel;
}
}

/**Table Model**/

package com.akshay.spring.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student_details")
public class StudentModel {
@Id
@GeneratedValue(strategy =GenerationType.AUTO)
@Column(name = "id")
Long id;
@Column(name = "roll_number")
String rollNumber;

@Column(name = "first_name")
String firstName;

@Column(name = "last_name")
String lastName;

@Column(name = "standard")
String standard;

@Column(name = "phone_number")
String phoneNumber;

@Column(name = "email_id")
String emailId;

@Column(name = "fathers_name")
String fathersName;

@Column(name = "mothers_name")
String mothersName;

@Column(name = "address")
String address;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRollNumber() {
return rollNumber;
}
public void setRollNumber(String rollNumber) {
this.rollNumber = rollNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getFathersName() {
return fathersName;
}
public void setFathersName(String fathersName) {
this.fathersName = fathersName;
}
public String getMothersName() {
return mothersName;
}
public void setMothersName(String mothersName) {
this.mothersName = mothersName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "StudentDTO [id=" + id + ", rollNumber=" + rollNumber + ", firstName=" + firstName + ", lastName="
+ lastName + ", standard=" + standard + ", phoneNumber=" + phoneNumber + ", emailId=" + emailId
+ ", fathersName=" + fathersName + ", mothersName=" + mothersName + ", address=" + address + "]";
}

}
/**DTO**/

package com.akshay.spring.dtos;

public class StudentDTO {

Long id;
String rollNumber;
String firstName;
String lastName;
String standard;
String phoneNumber;
String emailId;
String fathersName;
String mothersName;
String address;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRollNumber() {
return rollNumber;
}
public void setRollNumber(String rollNumber) {
this.rollNumber = rollNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getFathersName() {
return fathersName;
}
public void setFathersName(String fathersName) {
this.fathersName = fathersName;
}
public String getMothersName() {
return mothersName;
}
public void setMothersName(String mothersName) {
this.mothersName = mothersName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "StudentDTO [id=" + id + ", rollNumber=" + rollNumber + ", firstName=" + firstName + ", lastName="
+ lastName + ", standard=" + standard + ", phoneNumber=" + phoneNumber + ", emailId=" + emailId
+ ", fathersName=" + fathersName + ", mothersName=" + mothersName + ", address=" + address + "]";
}



}

/**JPA repository**/

package com.akshay.spring.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import com.akshay.spring.models.StudentModel;

public interface StudentRepository extends JpaRepository<StudentModel, Long> {

StudentModel getStudentByRollNumber(String rollNumber);

void deleteStudentByRollNumber(String rollNumber);

}

最佳答案

我认为您的请求映射需要是:http://localhost:8081/api/getAllStudents

关于java - 无法访问 Spring Boot 的请求映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50773881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com