我有一个spring项目,我想转换项目。
这里是我的样本代码;
我的控制器
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private SchoolService schoolService;
@GetMapping("/list")
public String ListStudents(Model model) {
List<Student> theStudent = studentService.getStudents();
model.addAttribute("students", theStudent);
return "List-student";
}
@GetMapping("/addNewStudent")
public String addNewStudent(Model model) {
Student theStudent = new Student();
model.addAttribute("student", theStudent);
List<School> theSchool = schoolService.getSchools();
model.addAttribute("schools", theSchool);
return "student-form";
}
@PostMapping("/saveStudent")
public String saveStudent(@ModelAttribute("student") Student theStudent) {
studentService.saveStudent(theStudent);
return "redirect:/student/list";
}
@GetMapping("/showFormforUpdate")
public String showFormForUpdate(@RequestParam("studentID") int theId, Model model) {
Student theStudent = studentService.getStudent(theId);
model.addAttribute(theStudent);
List<School> theSchool = schoolService.getSchools();
model.addAttribute("schools", theSchool);
return "student-form";
}
@GetMapping("/deleteStudent")
public String deleteStudent(@RequestParam("studentID") int theId, Model model) {
studentService.deleteStudent(theId);
return "redirect:/student/list";
}
}我的DAOImpl课程
@Repository
public class StudenDAOImpl implements StudentDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public List<Student> getStudents() {
Session currentSession=sessionFactory.getCurrentSession();
Query<Student> theQuery = currentSession.createQuery("from Student", Student.class);
List<Student> students=theQuery.getResultList();
return students;
}
@Override
public void saveStudent(Student theStudent) {
Session currentSession=sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(theStudent);
}
@Override
public Student getStudent(int theId) {
Session currentSession=sessionFactory.getCurrentSession();
Student theStudent=currentSession.get(Student.class, theId);
return theStudent;
}
@Override
public void deleteStudent(int theId) {
Session currentSession=sessionFactory.getCurrentSession();
Query theQuery=currentSession.createQuery("delete from Student where id=:studentID");
theQuery.setParameter("studentID", theId);
theQuery.executeUpdate();
}
}其实我知道我需要改变的地方。但我不太清楚春天的休息阿皮。在我的代码中,我在视图(jsp文件)中发送了许多属性,并在这里捕捉到这些属性。但是RestApi没有视图。我需要删除属性函数。但是我可以添加什么而不是属性函数呢?我是新来的RestApi,请帮帮我,我该怎么办?
发布于 2019-07-03 13:12:16
欢迎来到堆栈溢出。
Rest的工作方式如下:
@GetMapping("/students")的函数)。REST接收请求,处理所述请求(如果存在请求数据),并使用状态代码提示返回一些数据,该状态代码指示操作是否成功。
对于春天,你会做这样的事情:
@GetMapping("/students")
ResponseEntity<List<Student>> listStudents() {
List<Student> stds = getStudents(); // call some service or DB
return new ResponseEntity<>(stds, HttpStatus.OK);
}当您向/students发出GET请求时,spring将将请求的处理委托给listStudents方法,该方法将获取学生并返回数据。
REST使用JSON,因此您返回的学生列表将被序列化为json列表。
如果要自定义学生json结构,可以使用Jackson:
public class Student {
@JsonProperty("cool_name") private String name;
// getters and setter
}rest不适用于视图或JSP。他们巧妙地处理http请求和响应。
如果您使用的是spring而不是spring,请查看本文:https://viralpatel.net/blogs/spring-4-mvc-rest-example-json/
如果您可以使用spring (我强烈推荐),请检查以下内容:https://spring.io/guides/gs/rest-service/
您还应该用@RestController对REST控制器进行注释,以便它们自动处理rest调用。
希望这有助于并欢迎堆栈溢出。
https://stackoverflow.com/questions/56870559
复制相似问题