Usage scenarios
In the process of interface development, when the Controller layer implements the interface switch for the front-end call, sometimes a function has been implemented in other controllers. For example, repeated implementation in the Controller will cause code redundancy, which is not the recommended scheme? How to make it easier for the front-end developers to look at it, and the back-end will not generate redundancy? It is recommended to use the forward implementation in the Controller.
Concrete realization
Here is an example. For example, in the MbrMemberCtroller, we have achieved the acquisition of member details. At this time, we need to develop an order Controller named OsmServiceOrderController. In this Controller, we need to obtain the supplier member details (that is, the member details implemented in the MbrMemberCtroller). How can we achieve this?
Original Controller implementation
MbrMemberCtroller
@ApiOperation(value = "Get member details", notes = "Get member details.", response = MbrMember.class, authorizations = {@Authorization(value="apikey")}) @ApiImplicitParams({ @ApiImplicitParam(name = "mbrMemberId", value = "Membership number", required = true, dataType = "string", paramType = "query") }) @Log("Detailed member information record") @PostMapping("detail/{mbrMemberId}") @RequiresPermissions("mbrMember:detail") public MbrMember detail(@NotBlank(message = "{required}") @PathVariable String mbrMemberId) throws FebsException { try { return this.iMbrMemberService.selectMbrMemberDetail(mbrMemberId); } catch (Exception e) { message = "Detailed member information record failed"; log.error(message, e); throw new FebsException(message); } }
Note that the above two Api annotations are used to generate Swagger interface documents, @ RequiresPermissions is the control of interface call permission, regardless of the focus here.
This is a post request interface.
Caller Controller implementation
@ApiOperation(value = "Detailed demander", notes = "Details of the employer.", response = String.class, authorizations = {@Authorization(value="apikey")}) @ApiImplicitParams({ @ApiImplicitParam(name = "mbrMemberId", value = "Member information No", required = true, dataType = "string", paramType = "query") }) @Log("Detailed order form") @PostMapping("demanderDetail/{mbrMemberId}") @RequiresPermissions("mbrMember:detail") public void demanderDetail(@NotBlank(message = "{required}") @PathVariable String mbrMemberId, HttpServletRequest req, HttpServletResponse resp) throws FebsException { try { req.getRequestDispatcher("/mbrMember/detail/" + mbrMemberId).forward(req, resp); } catch (Exception e) { message = "Agent detail demand side information failed"; log.error(message, e); throw new FebsException(message); } }
Here we use Request to call forward directly.
Forward can only be used in the same application, while redirect can be redirected to other external applications. Here is the implementation in the same application, with parameters, and the result set needs to be returned, so the forward method is more appropriate.