无法访问请求映射弹簧启动

时间:2018-06-09 11:38:25

标签: java spring spring-mvc spring-boot

这是我正在尝试从SQL DB使用RestControllers访问数据, 代码正在编译并且正常运行(Tomcat在端口上启动:8081(http),带有上下文路径'')但是每当我尝试从http://localhost:8081/getAllStudents上的邮递员访问映射时,我都会收到错误 {“timestamp”:“2018-06-09T11:03:59.136 + 0000”,“status”:404,“error”:“Not Found”,“message”:“没有可用消息”,“path”:“/ getAllStudents“}

/**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);

}

1 个答案:

答案 0 :(得分:1)

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