Spring Boot @Controller @RestController @RequestMapping @ResponseBody

Spring Boot @Controller @RestController @RequestMapping @ResponseBody

package com.idodo.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn 
 * Project: cts 
 * Module Name: HelloController
 * Comments: 控制器
 * JDK version used: JDK1.8 
 * Author: jzh 
 * Create Date: 2018-12-12
 * Modified By: jzh 
 * Modified Date: 2018-12-12
 * Why & What is modified:
 * Version: <1.0>
 */

@Controller
public class HelloController {
  
  /**
   * @Controller     Spring MVC注解,表示此类用于负责处理Web请求
   * @RequestMapping Spring MVC注解,表示如果请求路径匹配,被注解的方法将被调用
   * @ResponseBody   表示此方法返回的是文本而不是视图名称
   */
  
  @RequestMapping("/hello")
  public @ResponseBody String say(String name) {
    return "hello " + name;
  }
}
package com.idodo.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn 
 * Project: cts 
 * Module Name: LoginController
 * Comments: 控制器
 * JDK version used: JDK1.8 
 * Author: jzh 
 * Create Date: 2018-12-15
 * Modified By: jzh 
 * Modified Date: 2018-12-15
 * Why & What is modified:
 * Version: <1.0>
 */

@RestController
public class LoginController {

  /**
   * @RestController 相当于 @Controller 和 @ResponseBody
   * @RequestMapping Spring MVC注解,表示如果请求路径匹配,被注解的方法将被调用
   */
  
  @RequestMapping("/login")
  public String say(String name) {
    return "username: " + name;
  }
}

 

发表回复

您的电子邮箱地址不会被公开。