Getting and validating request parameters (Services)

The following Java code snippet demonstrates first retrieving two request parameters (param1 and param2) from the context Map passed to every OFBiz Service. Note that all request parameters start out life as strings of characters. The OFBiz Service engine will automatically convert request values to the appropriate type based on Service definition configuration.

How to do it...

The following code shows you how to retrieve parameters:

import java.util.*;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilValidate
public static Map myService(DispatchContext dctx, Map context){
// Some code here
// First, just get a request parameter from the context
// Note: this must already be configured as part of the
// Service definitions INPUT parameters
String param1 = (String) context.get("param1");
Integer param2 = (Integer) context.get("param2);
// You may use the UtilValidate methods to validate these.
if(UtilValidate.isNotEmpty(param1)) {
return ServiceUtil.returnError("ERROR Message here!");
// Use OFBiz
}
Map result = ServiceUtil.returnSuccess();
result.put("value1", "this is a value");
return result;
}

How it works...

For OFBiz Services, the context in which the Service operates is controlled by OFBiz. The only HTTP/HTTPS request parameters available to the Service are those that have been defined as IN parameters within the Service definition. To access any values that have been defined in Service definition, including request parameters, retrieve the value from the context Map as shown earlier.

Note

Note: Request parameters are ignored and not passed to a Service if the Service definition does not include an attribute entry with the request parameter's name and request parameter's mode="IN" or mode="INOUT".

Regardless of the contents of the map passed when a Service returns from execution, Service parameters not defined as mode="OUT" or mode="INOUT" are not made available to subsequent Service context consumers. In other words, if you have values that you would want returned to the context as a result of your Service's processing, declare them as mode="OUT" or mode="INOUT" in the Service definition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.37.136