@RequestMapping的简单理解

@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;
    获取路径参数.../item/*
    @RequestMapping("/item/{itemId}")
    @ResponseBody
//    HTTP Status 404 - /WEB-INF/jsp/item/536563.jsp
//    和晶晶的正相反,应该是导入数据库的问题
//    出现了manager的pom.xml文件忘记添加依赖common了
//此版本还删除了common中的一些包com.taotao.common,在commom的java包下,晶晶版保留
//    @PathVariable是从路径中取参数
    public TbItem getItemById(@PathVariable Long itemId) {
        TbItem tbItem = itemService.geTbItemById(itemId);
        return tbItem;
    }
}
    /**
     * 一般为首页/
     */
    @RequestMapping("/")
    public String showIndex() {
        return "index";
    }
    /**
     * 只要下面haha删除对应位置自定义名称一致就行,随便命名
     * @param page
     * @return
     */
    @RequestMapping("/{haha}")
    public String showpage(@PathVariable String haha) {
        return haha;
    }

知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。

1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

例如:

1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面

若返回json等内容到页面,则需要加@ResponseBody注解

@CrossOrigin
@Controller
public class FileUploadController {

//跳转到上传文件的页面
@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
public String goUploadImg() {
//跳转到 templates 目录下的 uploadimg.html
return "uploadimg";
}



//处理文件上传

@RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
System.out.println("调用文件上传方法");
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();

  2.@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

@CrossOrigin
@RestController /* @Controller + @ResponseBody*/
public class HospitalController {

    //注入Service服务对象
    @Autowired
    private HospitalService hospitalService;

    /**
     * 查询所有医院信息(未分页)
     */

    @RequestMapping(value = "findAllHospital",method = RequestMethod.GET)
    public  List<Hospital> findAllHospital(){
        List<Hospital> hospitalList= hospitalService.findAllHospital();
        return hospitalList;
    }
 
原文地址:https://www.cnblogs.com/dianzan/p/11100594.html