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.이 콘텐츠는 선택한 언어로 제공되지 않습니다.
3.2. Using JAX-RS APIs
3.2.1. Injecting data from a request URI 링크 복사링크가 클립보드에 복사되었습니다!
Overview 링크 복사링크가 클립보드에 복사되었습니다!
Getting data from the URI's path 링크 복사링크가 클립보드에 복사되었습니다!
javax.ws.rs.PathParam annotation. The @PathParam annotation has a single parameter that identifies the URI template variable from which the data will be injected.
@PathParam annotation specifies that the value of the URI template variable color is injected into the itemColor field.
Example 3.1. Injecting data from a URI template variable
@PathParam annotation are different from the ones described in the section called “Supported data types”. The entity into which the @PathParam annotation injects data must be of one of the following types:
PathSegmentThe value will be the final segment of the matching part of the path.List<PathSegment>The value will be a list ofPathSegmentobjects corresponding to the path segment(s) that matched the named template parameter.- primitives such as int, char, or long
- Objects that have a constructor that accepts a single String argument
- Objects that have a static
valueOf()method that accepts a single String argument
Using query parameters 링크 복사링크가 클립보드에 복사되었습니다!
?). They consist of one, or more, name value pairs where the name and value are separated by an equal sign(=). When more than one query parameter is specified, the pairs are separated from each other by either a semicolon(;) or an ampersand(&). Example 3.2, “URI with a query string” shows the syntax of a URI with query parameters.
Example 3.2. URI with a query string
http://fusesource.org?name=value;name2=value2;...
http://fusesource.org?name=value;name2=value2;...
javax.ws.rs.QueryParam annotation extracts the value of a query parameter and injects it into a JAX-RS resource. The annotation takes a single parameter that identifies the name of the query parameter from which the value is extracted and injected into the specified field, bean property, or parameter. The @QueryParam annotation supports the types described in the section called “Supported data types”.
id into the method's id parameter.
Example 3.3. Resource method using data from a query parameter
POST to /monstersforhire/daikaiju?id=jonas the updateMonster() method's type is set to daikaiju and the id is set to jonas.
Using matrix parameters 링크 복사링크가 클립보드에 복사되었습니다!
;). /mostersforhire/daikaiju;id=jonas has one matrix parameter called id and /monstersforhire/japan;type=daikaiju/flying;wingspan=40 has two matrix parameters called type and wingspan.
javax.ws.rs.MatrixParam annotation. The annotation takes a single parameter that identifies the name of the matrix parameter from which the value is extracted and injected into the specified field, bean property, or parameter. The @MatrixParam annotation supports the types described in the section called “Supported data types”.
type and id into the method's parameters.
Example 3.4. Resource method using data from matrix parameters
POST to /monstersforhire;type=daikaiju;id=whale the updateMonster() method's type is set to daikaiju and the id is set to whale.
Disabling URI decoding 링크 복사링크가 클립보드에 복사되었습니다!
javax.ws.rs.Encoded annotation to deactivate the URI decoding. The annotation can be used to deactivate URI decoding at the following levels:
- class level—Decorating a class with the
@Encodedannotation deactivates the URI decoding for all parameters, field, and bean properties in the class. - method level—Decorating a method with the
@Encodedannotation deactivates the URI decoding for all parameters of the class. - parameter/field level—Decorating a parameter or field with the
@Encodedannotation deactivates the URI decoding for all parameters of the class.
getMonster() method does not use URI decoding. The addMonster() method only disables URI decoding for the type parameter.
Example 3.5. Disabling URI decoding
Error handling 링크 복사링크가 클립보드에 복사되었습니다!
WebApplicationException exception wraps the original exception is generated. The WebApplicationException exception's status is set to 404.
3.2.2. Injecting data from the HTTP message header 링크 복사링크가 클립보드에 복사되었습니다!
Overview 링크 복사링크가 클립보드에 복사되었습니다!
Injecting information from the HTTP headers 링크 복사링크가 클립보드에 복사되었습니다!
javax.ws.rs.HeaderParam annotation is used to inject the data from an HTTP header field into a parameter, field, or bean property. It has a single parameter that specifies the name of the HTTP header field from which the value is extracted and injected into the resource implementation. The associated parameter, field, or bean property must conform to the data types described in the section called “Supported data types”.
If-Modified-Since header into a class' oldestDate field.
Example 3.6. Injecting the If-Modified-Since header
Injecting information from a cookie 링크 복사링크가 클립보드에 복사되었습니다!
javax.ws.rs.CookieParam annotation extracts the value from a cookie's field and injects it into a resource implementation. It takes a single parameter that specifies the name of the cookie's field from which the value is to be extracted. In addition to the data types listed in the section called “Supported data types”, entities decorated with the @CookieParam can also be a Cookie object.
handle cookie into a field in the CB class.
Example 3.7. Injecting a cookie
Error handling 링크 복사링크가 클립보드에 복사되었습니다!
WebApplicationException exception wrapping the original exception is generated. The WebApplicationException exception's status is set to 400.
3.2.3. Injecting data from HTML forms 링크 복사링크가 클립보드에 복사되었습니다!
Overview 링크 복사링크가 클립보드에 복사되었습니다!
GET requests and HTTP POST requests:
- GET
- When form data is sent as part of an HTTP
GETrequest the data is appended to the URI as a set of query parameters. Injecting data from query parameters is discussed in the section called “Using query parameters”. - POST
- When form data is sent as part of an HTTP
POSTrequest the data is placed in the HTTP message body. The form data can be handled using a regular entity parameter that supports the form data. It can also be handled by using the@FormParamannotation to extract the data and inject the pieces into resource method parameters.
Using the @FormParam annotation to inject form data 링크 복사링크가 클립보드에 복사되었습니다!
javax.ws.rs.FormParam annotation extracts field values from form data and injects the value into resource method parameters. The annotation takes a single parameter that specifies the key of the field from which it extracts the values. The associated parameter must conform to the data types described in the section called “Supported data types”.
@FormParam annotation can be placed on fields, methods, and parameters. However, the @FormParam annotation is only meaningful when placed on resource method parameters.
Example 링크 복사링크가 클립보드에 복사되었습니다!
title, tags, and body—that contain string data.
Example 3.8. Injecting form data into resource method parameters
3.2.4. Specifying a default value to inject 링크 복사링크가 클립보드에 복사되었습니다!
Overview 링크 복사링크가 클립보드에 복사되었습니다!
javax.ws.rs.DefaultValue annotation can be used in conjunction with the following injection annotations:
@PathParam@QueryParam@MatrixParam@FormParam@HeaderParam@CookieParam
@DefaultValue annotation specifies a default value to be used when the data corresponding to the injection annotation is not present in the request.
Syntax 링크 복사링크가 클립보드에 복사되었습니다!
@DefaultValue annotation.
Example 3.9. Syntax for setting the default value of a parameter
@DefaultValue annotation relative to the accompanying injection annotation does not matter.
@DefaultValue annotation takes a single parameter. This parameter is the value that will be injected into the field if the proper data cannot be extracted based on the injection annotation. The value can be any String value. The value should be compatible with type of the associated field. For example, if the associated field is of type int, a default value of blue results in an exception.
Dealing with lists and sets 링크 복사링크가 클립보드에 복사되었습니다!
List, Set, or SortedSet then the resulting collection will have a single entry mapped from the supplied default value.
Example 링크 복사링크가 클립보드에 복사되었습니다!
@DefaultValue to specify a default value for a field whose value is injected.
Example 3.10. Setting default values
getMonster() method in Example 3.10, “Setting default values” is invoked when a GET request is sent to baseURI/monster. The method expects two query parameters, id and type, appended to the URI. So a GET request using the URI baseURI/monster?id=1&type=fomóiri would return the Fomóiri with the id of one.
@DefaultValue annotation is placed on both parameters, the getMonster() method can function if the query parameters are omitted. A GET request sent to baseURI/monster is equivalent to a GET request using the URI baseURI/monster?id=42&type=bogeyman.