SpringBoot
@Bean 啟動加載的Function不能同名
即使不在同一個Class裡面的Function,如果同名則不能啟動兩個,SpringBoot會隨機啟動一個。
/**
* Load all of the CRM company
*/
@Bean
public void loadCrmCompany() {
CRM_CompanyLst = crmCompanyService.queryAllCompany();
}
启动application
java -jar target\abc.jar --spring.profiles.active=dev
设置profile
Argument
--spring.profiles.active=dev
VM arguments
-Dspring.profiles.active=dev
Environment
spring.profiles.active = dev
SpringBoot 请求的方式
POST application/json 以对象接收
客户端
{
"userName": "michael",
"userPasswd":"123456"
}
服务器端
@RequestMapping("/user/getUserInfo")
public UserVO getLogin(@RequestBody UserVO user)
POST application/json 以Map接收
通常参数比较少的情况下使用这种方式
客户端
{
"userId": "1"
}
服务器端
@PostMapping("/user/logout")
public UserVO logout(@RequestBody Map user) {
long userId = Long.valueOf(user.get("userId").toString());
POST form-data
客户端
服务器端
@RequestParam(value="userId",required=true) long id,
@RequestParam(value="password") String userPasswd
GET
客户端
http://127.0.0.1:8888/rest/user/getUserById/1
服务器端
@GetMapping("/user/getUserById/{id}")
public UserVO getUserById(@PathVariable("id") long id)
PUT application/json 以对象接收
客户端
{
"id": 7,
"orderName": "TSL broken",
"des": "Eloon Mask is a superman",
"time": "2023-05-05 10:00:00"
}
服务器端
@PutMapping("/order")
public void updateOrder(@RequestBody OrderVO order)
Delete
客户端
服务器端
@DeleteMapping("/order/{id}")
public void DeleteOrder(@PathVariable Long id) {
}
SpringBoot 返回值
SpringBoot Response 的Items
config
headers
Appcept 接收的数据格式,如 application/json, text/plain
Authorization Token
request-time 请求的时间
method
POST,GET,OPTIONS,DELETE,HEAD
timeout
timeout 和 request-time 的层级不同
url
validateStatus
withCredentials
xsrfCookieName
xsrfHeaderName
data
服务器端返回的数据,原始对象或者封装后的对象。
header
access-control-allow-credentials: "true"
access-control-allow-headers:
"Authorization,Orign,
X-Requested-With,Content-Type,
Accept,Accept,Access-Token,
access-control-allow-origin,
authorization,
request-time,
x-custom-header,
authority,version-info"
access-control-allow-methods: "POST,GET,OPTIONS,DELETE,HEAD"
access-control-allow-origin: "http://localhost:5173"
access-control-max-age: "86400"
connection: "close"
content-type: "application/json;charset=UTF-8"
date: "Tue, 18 Jul 2023 08:57:42 GMT"
transfer-encoding: "chunked"
vary: "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
request
response
responseText
responseType
responseURL
这部分冗余,在response 对象下也有这两个值
status: 200
statusText “OK”
status
statusText
[[Prototype]] Object
没有封装返回值
@Slf4j
@RestController
@RequestMapping("/rest")
public class MenuControl implements WebContext{
@Resource
MenuMapper menuMapper;
@PostMapping("/menu/getAllMenu")
public List<Menu> getAllMenu() {
return menuMapper.queryAllMenu();
}
}
封装返回值
@Slf4j
@Component
@ControllerAdvice
public class RestResponseBodyAdvice implements ResponseBodyAdvice {
@Override
public Object beforeBodyWrite(Object body,
MethodParameter returnType,
MediaType mediaType,
Class selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response) {
log.info("{} {}",RestResponseBodyAdvice.class,request.getURI());
if(mediaType.equalsTypeAndSubtype(MediaType.APPLICATION_JSON)) {
if(body instanceof AjaxResponse) {
//if body is AjaxResponse, make both of Ajax code and Http code to be the same
response.setStatusCode(HttpStatusCode.valueOf(((AjaxResponse)body).getCode()));
}else {
//if body is not AjaxResponse, make it to be AjaxResponse type.
if(body instanceof EntityVO || body instanceof List){
response.setStatusCode(HttpStatus.OK);
AjaxResponse jsonObj = AjaxResponse.success(body);
log.info("{} jsonObj:{}",RestResponseBodyAdvice.class,jsonObj.toString());
return jsonObj;
}
}
}
return body;
}
}
@RequestBody Json
RequestBody 自动把客户端的Json对象转成成POJO,发生Unrecognized field时,说明两者不能对象。
比如名字错误
JSON parse error: Unrecognized field "menuShown" (class com.book.erp.entity.menu.Menu), not marked as ignorable