Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.이 콘텐츠는 선택한 언어로 제공되지 않습니다.
37.3. Widget Vendor Example
37.3.1. Widget Ordering Interface 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
This section shows an example of substitution groups being used in Apache CXF to solve a real world application. A service and consumer are developed using the widget substitution group defined in Example 37.2, “Substitution Group with Complex Types”. The service offers two operations:
checkWidgets
and placeWidgetOrder
. Example 37.12, “Widget Ordering Interface” shows the interface for the ordering service.
Example 37.12. Widget Ordering Interface
Example 37.13, “Widget Ordering SEI” shows the generated Java SEI for the interface.
Example 37.13. Widget Ordering SEI
Note
Because the example only demonstrates the use of substitution groups, some of the business logic is not shown.
37.3.2. The checkWidgets Operation 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
Overview 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
checkWidgets
is a simple operation that has a parameter that is the head member of a substitution group. This operation demonstrates how to deal with individual parameters that are members of a substitution group. The consumer must ensure that the parameter is a valid member of the substitution group. The service must properly determine which member of the substitution group was sent in the request.
Consumer implementation 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The generated method signature uses the Java class supporting the type of the substitution group's head element. Because the member elements of a substitution group are either of the same type as the head element or of a type derived from the head element's type, the Java classes generated to support the members of the substitution group inherit from the Java class generated to support the head element. Java's type hierarchy natively supports using subclasses in place of the parent class.
Because of how Apache CXF generates the types for a substitution group and Java's type hierarchy, the client can invoke
checkWidgets()
without using any special code. When developing the logic to invoke checkWidgets()
you can pass in an object of one of the classes generated to support the widget substitution group.
Example 37.14, “Consumer Invoking
checkWidgets()
” shows a consumer invoking checkWidgets()
.
Example 37.14. Consumer Invoking checkWidgets()
Service implementation 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The service's implementation of
checkWidgets()
gets a widget description as a WidgetType
object, checks the inventory of widgets, and returns the number of widgets in stock. Because all of the classes used to implement the substitution group inherit from the same base class, you can implement checkWidgets()
without using any JAXB specific APIs.
All of the classes generated to support the members of the substitution group for
widget
extend the WidgetType
class. Because of this fact, you can use instanceof
to determine what type of widget was passed in and simply cast the widgetPart
object into the more restrictive type if appropriate. Once you have the proper type of object, you can check the inventory of the right kind of widget.
Example 37.15, “Service Implementation of
checkWidgets()
” shows a possible implementation.
Example 37.15. Service Implementation of checkWidgets()
37.3.3. The placeWidgetOrder Operation 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
Overview 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
placeWidgetOrder
uses two complex types containing the substitution group. This operation demonstrates to use such a structure in a Java implementation. Both the consumer and the service must get and set members of a substitution group.
Consumer implementation 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
To invoke
placeWidgetOrder()
the consumer must construct a widget order containing one element of the widget substitution group. When adding the widget to the order, the consumer should use the object factory methods generated for each element of the substitution group. This ensures that the runtime and the service can correctly process the order. For example, if an order is being placed for a plastic widget, the ObjectFactory.createPlasticWidget()
method is used to create the element before adding it to the order.
Example 37.16, “Setting a Substitution Group Member” shows consumer code for setting the widget property of the
WidgetOrderInfo
object.
Example 37.16. Setting a Substitution Group Member
Service implementation 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
The
placeWidgetOrder()
method receives an order in the form of a WidgetOrderInfo
object, processes the order, and returns a bill to the consumer in the form of a WidgetOrderBillInfo
object. The orders can be for a plain widget, a plastic widget, or a wooden widget. The type of widget ordered is determined by what type of object is stored in widgetOrderForm
object’s widget property. The widget property is a substitution group and can contain a widget
element, a woodWidget
element, or a plasticWidget
element.
The implementation must determine which of the possible elements is stored in the order. This can be accomplished using the
JAXBElement<? extends T>
object's getName()
method to determine the element's QName. The QName can then be used to determine which element in the substitution group is in the order. Once the element included in the bill is known, you can extract its value into the proper type of object.
Example 37.17, “Implementation of
placeWidgetOrder()
” shows a possible implementation.
Example 37.17. Implementation of placeWidgetOrder()
The code in Example 37.17, “Implementation of
placeWidgetOrder()
” does the following:
- 1
- Instantiates an object factory to create elements.
- 2
- Instantiates a
WidgetOrderBillInfo
object to hold the bill. - 3
- Gets the number of widgets ordered.
- 4
- Gets the local name of the element stored in the order.
- 5
- Checks to see if the element is a
woodWidget
element. - 6
- Extracts the value of the element from the order to the proper type of object.
- 7
- Creates a
JAXBElement<T>
object placed into the bill. - 8
- Sets the bill object's widget property.
- 9
- Sets the bill object's amountDue property.