stereotype.Controller 和 RestController 差别
时间: 2023-12-05 07:39:27 浏览: 27
stereotype.Controller和RestController的主要区别在于@RestController注解相当于@ResponseBody + @Controller合在一起的作用。也就是说,使用@RestController注解的类中的方法都会以json格式返回数据,而不是返回视图。而使用@Controller注解的类中的方法则需要配合视图解析器才能返回视图。因此,如果你的项目中需要返回json格式的数据,那么建议使用@RestController注解。如果需要返回视图,则使用@Controller注解。
下面是一个使用@RestController注解的例子:
```java
@RestController
public class MyRestController {
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}
}
```
下面是一个使用@Controller注解的例子:
```java
@Controller
public class MyController {
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello World!");
return "hello";
}
}
```
相关问题
springboot的@RestController跳jsp页面
在Spring Boot中,使用`@RestController`注解的控制器默认返回的是JSON数据,而不是跳转到JSP页面。如果你想要在`@RestController`中跳转到JSP页面,可以使用`ModelAndView`对象来实现。
下面是一个示例代码:
```java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class JspController {
@GetMapping("/jsp")
public ModelAndView jspPage() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("your-jsp-page"); // 设置要跳转的JSP页面的名称
return modelAndView;
}
}
```
在上面的示例中,我们创建了一个`JspController`类,并使用`@Controller`注解标记为控制器。然后,我们在`jspPage()`方法上使用`@GetMapping`注解来指定访问该方法的URL路径。在方法中,我们创建了一个`ModelAndView`对象,并使用`setViewName()`方法设置要跳转的JSP页面的名称。
请注意,为了使Spring Boot能够正确地找到JSP页面,你需要在`src/main/resources`目录下创建一个`templates`文件夹,并将JSP页面放在该文件夹中。
mabitis-plus查询所有 service controller mapper分别写什么
好的,我来为您分别编写Mybatis-Plus查询所有分类的service、controller、mapper代码。
1. Service层代码:
```
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Category;
import com.example.demo.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CategoryService {
@Autowired
private CategoryMapper categoryMapper;
public List<Category> getAllCategories() {
QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
List<Category> categoryList = categoryMapper.selectList(queryWrapper);
return categoryList;
}
}
```
2. Controller层代码:
```
import com.example.demo.entity.Category;
import com.example.demo.service.CategoryService;
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;
import java.util.List;
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping("/list")
public List<Category> getAllCategories() {
return categoryService.getAllCategories();
}
}
```
3. Mapper层代码:
```
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Category;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}
```
在上述代码中,我们使用了Mybatis-Plus提供的BaseMapper接口,并传入了Category实体类作为泛型参数,来实现对Category表的基本增删改查操作。同时,我们在Mapper接口上使用了@Mapper注解,来声明这是一个Mapper接口。