controller 层在 MVC 设计属于控制层设计初衷:设计初衷:请求并响应请求;所以该层轻似接受,涉及业务的。
之前,使用下分开的开发设计模式,推荐使用
@RestController
注解它
@ResponseBody + @Controller
的组合。
-
如果只是将Controller中的内容解开,
@RestController
则将Controller中的内容解析器的视图的解法,或者将配置方法重新设置为返回使用HTML格式的方法,或者返回解析器返回常用的方法
InternalResourceViewResolver
,返回js的内容。
-
如果需要返回到指定的页面,则需要用
@Controller
视图来指定解析器
InternalResourceViewResolver
才行。
mediaType``@ResponseBody
如,使用
@Controller
注释解,在的方法上,查看解析器可以解析返回的jsp,html页面,跳转到相应页面;若返回json等内容到页面,则需要加
@ResponseBody
注解
使用注解
@PostMapping("/page")
,类命名和方法除掉都可以加。
注意按照不同业务划分使用,避免乱写乱用。
常用的POST/GET。使用注解:
@RequestMapping
和
@GetMapping
@PostMapping
。
4.3中的介绍
@GetMapping
、介绍
@PostMapping
、提高的方法、
@PutMapping
来帮助解决快速表达方式的HTTP和地
@DeleteMapping``@PatchMapping
该注解
HTTP Get
方法将映射到特定的处理上
-
@GetMapping
是一个注释解,它是一个组合
@RequestMapping(method = RequestMethod.GET)
的缩写
-
@PostMapping
是一个注释解,它是一个组合
@RequestMapping(method = RequestMethod.POST)
的缩写
①提交提交,直接使用vo类或具体参数名接收;
@Controller
public class LoginController {
@RequestMapping(value = "login", method = RequestMethod.POST)
public String login(UserVO user){
System.out.println("POJO: " + user.getClass().getName() +
", hash code: " + user.hashCode() + ", " + user.toString());
return "redirect:/";
}
}
②@RequestParam
@RequestParam(value="", required=true, defaultValue="")
@RequestParam
有三个属性:
-
-
required:
必须,默认为true 请求中必须包含该参数,如果包含没有,即抛出异常(可选配置)
-
defaultValue:
默认值,如果设置了该值,必填 将自动设为 false
@ApiOperation(value = "根据id查询")
@PostMapping("/show")
public Responses show(@RequestParam(value="userId",defaultValue="-1") Long userId) {
Record data = recordService.getOne(vo.getId());
return Responses.success(data);
}
③n提交,使用注解@RequestBody
@RequestBody`主要接收接收端以POST方式传递给使用`@RequestBody`数据时(JSON字符串中的数据请求体中的数据的);GET方式无请求体,所以接收端不能使用GET方式提交数据,只是可以用同一个POST方式进行提交。可以在同一个接收方法中,`@RequestBody`最多可以有一个,并且有多个。`@RequestParam()``@RequestBody``@RequestParam()
注:一个请求,只有一个
RequestBody
请求,可以有多个
RequestParam
。
@ApiOperation(value = "根据id查询")
@PostMapping("/get")
public Responses getOne(@Validated @RequestBody IdVO vo){
Record data = recordService.getOne(vo.getId());
return Responses.success(data);
}
④ath变量
@RestController
@RequestMapping("/")
public class ChineseDrugController {
@ResponseBody
@RequestMapping(value = "/{name}")
public String showName(@PathVariable String name, @PathVariable(value = "name", required = false) String sex) {
return "Hello " + name + sex;
}
⑤@PathParam
url:http://127.0.0.1:8080/sexvalue/namevalue?name=唐&sex=男
@RestController
@RequestMapping(value = "/{sex}")
public class ChineseDrugController {
@ResponseBody
@RequestMapping(value = "/{name}")
public String showName(@PathVariable(value = "name") String name, @PathParam(value = "sex") String sex) {
return "Hello " + name + " " + sex;
}
}
说明: 示例代码的实用性更高,实际开发中使用了各种功能。
参数参数
-
使用注意解说
@Validated
,有特色的自动评测开始了,它是
spring-contex
中性的注释解说;
-
vo类中自定义标注,比如
@NotNull
下等,他是javax
validation-api
中的注解这里不赘述;
-
示例方法如下
@ApiOperation(value = "应用类型和应用关系绑定")
@PostMapping("/applicationTypeBind")
public Boolean applicationTypeBind(@Validated @RequestBody ApplicationBindVO vo){
applicationTypeService.applicationTypeBind(vo);
return true;
}
VO 类示例
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;
@Data
@ApiModel(value = "ApplicationBindVO",description = "关系绑定vo")
public class ApplicationBindVO {
@NotNull
@ApiModelProperty("应用类型id")
private Long typeId;
@ApiModelProperty("应用id集合")
private List applicationIdList;
}
到期业务而定,格式轻松统一;
响应前端(APP/PC)的参数,一般重新处理,按顺序排列,方便统一
Responses.success(data);
import com.fasterxml.jackson.annotation.JsonView;
import com.myfutech.common.util.enums.ResponseCode;
import com.myfutech.common.util.vo.BaseView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "Responses",description = "响应信息")
public class Responses<T> {
@JsonView({BaseView.class})
@ApiModelProperty("响应编码")
private String code;
@JsonView({BaseView.class})
@ApiModelProperty("响应消息")
private String msg;
@JsonView({BaseView.class})
@ApiModelProperty("响应体")
private T result;
public static Responses success() {
return new Responses(ResponseCode.SUCCESS_CODE, "", (Object)null);
}
public static Responses success(T result) {
return new Responses(ResponseCode.SUCCESS_CODE, "", result);
}
public static Responses success(String msg, T result) {
return new Responses(ResponseCode.SUCCESS_CODE, msg, result);
}
public static Responses error(String msg) {
return new Responses(ResponseCode.ERROR_CODE, msg, (Object)null);
}
public static Responses error(ResponseCode code) {
return new Responses(code, code.getDefaultMsg(), (Object)null);
}
public static Responses error(ResponseCode code, String msg) {
return new Responses(code, msg, (Object)null);
}
public Responses() {
}
private Responses(ResponseCode code, String msg, T result) {
this.code = code.getCode();
this.msg = msg;
this.result = result;
}
public String getCode() {
return this.code;
}
public boolean notSuccess() {
return !ResponseCode.SUCCESS_CODE.getCode().equals(this.code);
}
public String getMsg() {
return this.msg;
}
public T getResult() {
return this.result;
}
public void setCode(String code) {
this.code = code;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setResult(T result) {
this.result = result;
}
}
使用SwaggerAPI,常用注解
//加载类名之上
@Api(tags = "日志相关接口", description="操作日志",
consumes= MediaType.APPLICATION_JSON_UTF8_VALUE,
produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
//加在方法名之上