系统定制开发RestTemplate发送数据为JSON的Post请求

目录

零、参考文献

一、写在前面

系统定制开发最近写一个小游戏,系统定制开发需要服务端向匹配端发送Json数据,系统定制开发记录业务端需要向服务端用,且数据为JSON格式时的请求方式

二、问题场景

业务端需要请求服务端的 /player/add 接口,但是 /player/add 接口接收的是 JSON 数据,即接口参数被 @RequestBody 注解修饰。

三、场景重现

业务端代码

请求对象的Url,以本地为例

public interface MatchingConstant {  	/**  	 * 匹配系统 添加玩家的 Url  	 */	 String addPlayerUrl = "http://127.0.0.1:3001/player/add";  	  	/**  	 * 匹配系统 移除玩家的 Url  	 */	 String removePlayerUrl = "http://127.0.0.1:3001/player/remove";	 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

websocket请求转发逻辑

@Component  @ServerEndpoint("/websocket/{token}")  @Slf4j  public class WebSocketServer {	private User user;		private void startMatching() {      log.info("======> start Matching");      MatchingRequest matchingRequest = new MatchingRequest(this.user.getId(), this.user.getRating());        Map<String, String> requestBody = new HashMap<>();  	requestBody.put("userId", String.valueOf(this.user.getId()));  	requestBody.put("rating", String.valueOf(this.user.getRating())); 	 	HttpHeaders requestHeaders = new HttpHeaders();  	// 重点是配置请求头内容类型为:"application/json"	requestHeaders.setContentType(MediaType.APPLICATION_JSON);  	HttpEntity<Map<String, String>> r = new HttpEntity<>(requestBody, requestHeaders);  		// 请求服务端添加玩家	String data= restTemplate.postForObject(MatchingConstant.addPlayerUrl, r, String.class);	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

服务端代码

接收参数的实体类

@Data  @AllArgsConstructor  @NoArgsConstructor  // 无参构造用于反序列化public class MatchingRequest {       private int userId;       private int rating;       public MatchingRequest(int userId) {        this.userId = userId;     }  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

服务端Controller层接口

@RestController  @RequestMapping("/player/")  public class MatchingController {  	/**  	 * service 层代码略  	 */    @Resource      private MatchingService matchingService;        @PostMapping("/add")      public String addPlayer(@RequestBody MatchingRequest matchingRequest ) {          return matchingService.addPlayer(matchingRequest);      }    	@PostMapping("/remove")  	public String removePlayer(@RequestBody MatchingRequest matchingRequest) {  	    return matchingService.removePlayer(matchingRequest);  	}  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

其余层代码略

四、总结反思

  1. 通过RestTemplate发送Post请求,请求体为Json或其他非文本格式,需要更改请求体中 ContentType
  2. 接收请求的实体类需要无参构造方法,不然无法反序列化,详见:

五、写在后面

欢迎关注,实现期间会经常记录一些项目实践中遇到的问题。

欢迎随时留言讨论,与君共勉,知无不答!

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发