Ofbiz project-based learning - Summary stage - service returns results

A return to success

1, the preparation of [service] returnSuccess in .DispatcherReturnDemoService class, reads as follows:

/ ** 
     * Returns a successful result 
     * @param dctx 
     * @param context 
     * @return 
     * / 
	public static the Map <String, Object> returnSuccess (DispatchContext dctx, the Map <String, Object> context) {		 
           
		// fill in the code execution service 
		/ / ....... 
		String Message = "successful implementation"; 
		
		// if successful when the program is executed, the result returns a successful set 
		the Map <String, Object> = ReturnMapUtil.getSuccessMap result (); 
		result.put ( "Message", Message); 
		return Result;		 
}

2, in services_dispatcher_return_demo.xml servicedef folder in these two methods formulated into service

	<service name="returnSuccess" engine="java"
		location="DispatcherReturnDemoService" invoke="returnSuccess">
		<attribute name="returnCode" type="Map" mode="OUT" optional="true" />
	</service>

Second, failure to return

1, failure result is returned

1.1 returnError] [writing service in DispatcherReturnDemoService class, reads as follows:

/ ** 
     * Returns failure results 
     * @param dctx 
     * @param context 
     * @return 
     * / 
	public static the Map <String, Object> returnError (DispatchContext dctx, the Map <String, Object> context) { 
		
           
		// fill in the code execution service 
		/ / ....... 
		String Message = "failed"; 
		Boolean = ErrorFlag to true; 
		
		// return result 
		the Map <String, Object> result = null; 
		iF (ErrorFlag) { 
			// when the program fails, returns a success result set 
			result = ReturnMapUtil.getErrorMap ( "errorCode") ; errorCode // here with specific error code needs to be replaced 
			result.put ( "Message", Message); 
		} the else { 
			// the program when executed successfully, return success result set 
			result = ReturnMapUtil.getSuccessMap (); 
			result.put ( "Message", message); 
		} 
		return result;
		
}

1.2 services_dispatcher_return_demo.xml servicedef folder in these two methods formulated into service

	<service name="returnError" engine="java"
		location="DispatcherReturnDemoService" invoke="returnError">
		<attribute name="returnCode" type="Map" mode="OUT" optional="true" />
	</service>

2, after an internal service failed to catch exceptions how to turn the results returned

2.1 DispatcherReturnDemoService writing class service [catchError], reads as follows:

/ ** 
     * capture abnormal results 
     * @param dctx 
     * @param context 
     * @return 
     * / 
	public static the Map <String, Object> catchError (DispatchContext dctx, the Map <String, Object> context) { 
		
		// fill in the code execution services to 
		try { 
			// code execution has assumed the case thrown back, we need to catch the exception 
			int. 8 = a; 
			int B = 0; 
			int I = a / B; 
			
		} the catch (exception E) { 
			// put error error information is printed to the log file level 
			Debug.logError (E, Module1); 
			// the description information corresponding to the specified error code returned to the service the caller 
			return ReturnMapUtil.getErrorMap (DemoErrorMapping.BASE0000); 
		} 
		// when the program is executed On failure, the result set returned successfully 
		return ReturnMapUtil.getSuccessMap (); 
		
}

2.2 services_dispatcher_return_demo.xml servicedef folder in these two methods formulated into service

	<service name="catchError" engine="java" location="DispatcherReturnDemoService" invoke="catchError">
		<attribute name="returnCode" type="Map" mode="OUT" optional="true" />
	</service>

3, after calling a service to determine how the service is successfully executed

3.1 Writing in DispatcherReturnDemoService class service [checkResult], reads as follows:

/ ** 
     * After calling a service how to determine whether the service is executed successfully 
     * @param dctx 
     * @param context 
     * @return 
     * / 
	public static the Map <String, Object> checkResult (DispatchContext dctx, the Map <String, Object> context) { 
		
         
        // get the service engine 
		LocalDispatcher the Dispatcher = dctx.getDispatcher (); 
		
		
		// ..... 
		// other code 
		// ....... 
		
		// call the service 
		the Map <String, Object> the Output = null; 
		the try { 
			map <String, Object> = FastMap.newInstance INPUT (); 
			Output = dispatcher.runSync ( "returnSuccess", INPUT);	 
		} the catch (Exception E) { 
			// error level to put the print error information to the log file 
			Debug. logError (E, Module1); 
			// the description information corresponding to the specified error code returned to the service the caller
			ReturnMapUtil.getErrorMap return (DemoErrorMapping.BASE0000); 
		} 
		// check whether the service has been performed successfully, if not successfully implemented, put the wrong result directly back to the upper caller. 
		IF {(ServiceUtil.isSuccess (the Output)!) 
			return the Output ; 
		} 
		// ..... 
		// other code 
		// ....... 
		
		@ when the program is executed successfully, a successful return result sets 
		return ReturnMapUtil.getSuccessMap ();	 
}

3.2 services_dispatcher_return_demo.xml servicedef folder in these two methods formulated into service

	<service name="returnError" engine="java" location="DispatcherReturnDemoService" invoke="returnError">
		<attribute name="returnCode" type="Map" mode="OUT" optional="true" />
	</service>

4, how to return the result of the failure of a service turn into business exceptions can be thrown

4.1 DispatcherReturnDemoService written in a class method [checkResult], reads as follows: 

/ ** 
     * how the failure of a service returns the results turn into business exceptions can be thrown (this is only a method, not a service) 
     * @param dctx 
     * @param context 
     * @return 
     * / 
	public static void returnInMethod (LocalDispatcher the Dispatcher ) throws BusinessException { 
		
		// ..... 
		// other code 
		// ....... 
		
		// call service 
		the Map <String, Object> Output = null; 
		the try { 
			the Map <String, Object> = fastmap INPUT. the newInstance (); 
			Output = dispatcher.runSync ( "returnSuccess", INPUT);	 
		} the catch (Exception E) { 
			// error level to put the print error information to the log file 
			Debug.logError (E, Module1); 
			// put exception type turn into the specified type, and throw 
			throw new BusinessException (e, DemoErrorMapping.BASE0000) ; 
		}
		// Check whether the service has been performed successfully, if not successfully implemented, the result is returned turn into business exception classes thrown 
		IF (ServiceUtil.isSuccess (the Output)!) { 
			The throw new new BusinessException (the Output); 
		} 
		
		// .... . 
		// other code 
		// ....... 
}

  

 

  

 

Guess you like

Origin www.cnblogs.com/gzhcsu/p/11202619.html