SpringBoot Manual

本文详细介绍了如何在SpringBoot应用中设置不同的profile,以及各种HTTP请求方式的应用示例,包括POST JSON、Map、form-data、GET、PUT和DELETE,并探讨了返回值的处理和常用HTTP头的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值