如何用@restcontroller替换@controller?

3yhwsihp  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(339)

这是我的 TeacherController :

@Controller
@RequestMapping("/teacherController")
public class TeacherController {
private static final String TEACHER_MODEL = "teacher";
@Autowired
TeacherService teacherService;

@RequestMapping("check")
public ModelAndView index() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("teachers", teacherService.getAll());
    modelAndView.setViewName("/teacherViews/teachersPage");
    return modelAndView;
}

这里是我打印表格的页面:

<table  class="table table-dark" border="1" width="100%" cellpadding="5">
<thead>
<tr>
    <th>Teacher ID</th>
    <th>Teacher Name</th>
    <th>Teacher Position</th>
</tr>
</thead>
<tbody id="teacherBody">
<tr th:each="teacher : ${teachers}">
    <td th:text="${teacher.teacherId}" />
    <td th:text="${teacher.teacherName}" />
    <td th:text="${teacher.position}" />
</tr>
</tbody>

我上面写的所有内容都需要使用restcontroller和ajax重写。我希望表数据加载页面自动加载,据我所知,它可以与 js .
所以,我的问题是我需要用ajax将我的控制器重写为restcontroller,但我不知道怎么做。
提前感谢您的帮助!!!

yhxst69z

yhxst69z1#

@controller和@rest controller的区别
@控制器
@controller注解已经成为框架的一部分很长时间了。@controller注解指定被注解的类是控制器。它是@controller的专门化,通过类路径扫描自动检测。它通常用于与基于@requestmapping注解的带注解处理程序方法的合并。
例如,

@Controller
@RequestMapping("users")
public class HomeController {

    @GetMapping(produces = "application/json")
    public @ResponseBody User getAllUsers() {
        return findAllUsers();
    }
}

请求处理方法用@responsebody注解。此注解支持将返回对象自动序列化到httpresponse中。
@REST控制器
@restcontroller注解在spring4.0中引入,以简化restfulweb服务的产生。它是一个方便的注解,结合了@controller和@responsebody
例如,

@RestController
@RequestMapping("users")
public class HomeController {

    @GetMapping(produces = "application/json")
    public Book getAllUsers() {
        return findAllUsers();
    }
}

控制器使用@restcontroller注解进行注解,因此不需要@responsebody。控制器类的每个请求处理方法都自动将返回对象序列化为httpresponse。链接
转换后
REST控制器

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @GetMapping("/teacher")
    public List<Teacher> fetchAllTeacher() {
        return teacherService.getAll();
    }
}

html格式

<table id="table-content" border='1'>
<tr>
    <th>Teacher ID</th>
     <th>Teacher Name</th>
    <th>Teacher Position</th>
</tr>

javascript(未测试)

var service = 'http://localhost/api/v1/';

$(document).ready(function(){

    jQuery.support.cors = true;

    $.ajax(
    {
        type: "GET",
        url: service + '/teacher/',
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function (data) {

        var trHTML = '';

        $.each(data, function (i, item) {

            trHTML += '<tr><td>' + data.Teacher[i].Name + '</td><td>' + data.Teacher[i].Position + '</td></tr>';
        });

        $('#table-content').append(trHTML);

        },

        error: function (msg) {

            alert(msg.responseText);
        }
    });
})

相关问题