Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* changed default query encoder result from POJO field to getter property * changed default query encoder result from POJO field to getter property * reset mistakenly deleted file * Create PropertyQueryMapEncoder and extract QueryMapEncoder.Default to FieldQueryMapEncoder * rename PropertyQueryMapEncoder to BeanQueryMapEncoder and add README * fix README * add comments to QueryMapEncoder and remove deprecation on Default * rename test name * rename package name queryMap to querymap * format code
- Loading branch information
Showing
8 changed files
with
429 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/src/main/java/feign/querymap/BeanQueryMapEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright 2012-2018 The Feign Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package feign.querymap; | ||
|
||
import feign.QueryMapEncoder; | ||
import feign.codec.EncodeException; | ||
import java.beans.IntrospectionException; | ||
import java.beans.Introspector; | ||
import java.beans.PropertyDescriptor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.*; | ||
|
||
/** | ||
* the query map will be generated using java beans accessible getter property as query parameter | ||
* names. | ||
* | ||
* eg: "/uri?name={name}&number={number}" | ||
* | ||
* order of included query parameters not guaranteed, and as usual, if any value is null, it will be | ||
* left out | ||
*/ | ||
public class BeanQueryMapEncoder implements QueryMapEncoder { | ||
private final Map<Class<?>, ObjectParamMetadata> classToMetadata = | ||
new HashMap<Class<?>, ObjectParamMetadata>(); | ||
|
||
@Override | ||
public Map<String, Object> encode(Object object) throws EncodeException { | ||
try { | ||
ObjectParamMetadata metadata = getMetadata(object.getClass()); | ||
Map<String, Object> propertyNameToValue = new HashMap<String, Object>(); | ||
for (PropertyDescriptor pd : metadata.objectProperties) { | ||
Object value = pd.getReadMethod().invoke(object); | ||
if (value != null && value != object) { | ||
propertyNameToValue.put(pd.getName(), value); | ||
} | ||
} | ||
return propertyNameToValue; | ||
} catch (IllegalAccessException | IntrospectionException | InvocationTargetException e) { | ||
throw new EncodeException("Failure encoding object into query map", e); | ||
} | ||
} | ||
|
||
private ObjectParamMetadata getMetadata(Class<?> objectType) throws IntrospectionException { | ||
ObjectParamMetadata metadata = classToMetadata.get(objectType); | ||
if (metadata == null) { | ||
metadata = ObjectParamMetadata.parseObjectType(objectType); | ||
classToMetadata.put(objectType, metadata); | ||
} | ||
return metadata; | ||
} | ||
|
||
private static class ObjectParamMetadata { | ||
|
||
private final List<PropertyDescriptor> objectProperties; | ||
|
||
private ObjectParamMetadata(List<PropertyDescriptor> objectProperties) { | ||
this.objectProperties = Collections.unmodifiableList(objectProperties); | ||
} | ||
|
||
private static ObjectParamMetadata parseObjectType(Class<?> type) | ||
throws IntrospectionException { | ||
List<PropertyDescriptor> properties = new ArrayList<PropertyDescriptor>(); | ||
|
||
for (PropertyDescriptor pd : Introspector.getBeanInfo(type).getPropertyDescriptors()) { | ||
boolean isGetterMethod = pd.getReadMethod() != null && !"class".equals(pd.getName()); | ||
if (isGetterMethod) { | ||
properties.add(pd); | ||
} | ||
} | ||
|
||
return new ObjectParamMetadata(properties); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
core/src/main/java/feign/querymap/FieldQueryMapEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright 2012-2018 The Feign Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package feign.querymap; | ||
|
||
import feign.QueryMapEncoder; | ||
import feign.codec.EncodeException; | ||
import java.lang.reflect.Field; | ||
import java.util.*; | ||
|
||
/** | ||
* the query map will be generated using member variable names as query parameter names. | ||
* | ||
* eg: "/uri?name={name}&number={number}" | ||
* | ||
* order of included query parameters not guaranteed, and as usual, if any value is null, it will be | ||
* left out | ||
*/ | ||
public class FieldQueryMapEncoder implements QueryMapEncoder { | ||
|
||
private final Map<Class<?>, ObjectParamMetadata> classToMetadata = | ||
new HashMap<Class<?>, ObjectParamMetadata>(); | ||
|
||
@Override | ||
public Map<String, Object> encode(Object object) throws EncodeException { | ||
try { | ||
ObjectParamMetadata metadata = getMetadata(object.getClass()); | ||
Map<String, Object> fieldNameToValue = new HashMap<String, Object>(); | ||
for (Field field : metadata.objectFields) { | ||
Object value = field.get(object); | ||
if (value != null && value != object) { | ||
fieldNameToValue.put(field.getName(), value); | ||
} | ||
} | ||
return fieldNameToValue; | ||
} catch (IllegalAccessException e) { | ||
throw new EncodeException("Failure encoding object into query map", e); | ||
} | ||
} | ||
|
||
private ObjectParamMetadata getMetadata(Class<?> objectType) { | ||
ObjectParamMetadata metadata = classToMetadata.get(objectType); | ||
if (metadata == null) { | ||
metadata = ObjectParamMetadata.parseObjectType(objectType); | ||
classToMetadata.put(objectType, metadata); | ||
} | ||
return metadata; | ||
} | ||
|
||
private static class ObjectParamMetadata { | ||
|
||
private final List<Field> objectFields; | ||
|
||
private ObjectParamMetadata(List<Field> objectFields) { | ||
this.objectFields = Collections.unmodifiableList(objectFields); | ||
} | ||
|
||
private static ObjectParamMetadata parseObjectType(Class<?> type) { | ||
List<Field> fields = new ArrayList<Field>(); | ||
for (Field field : type.getDeclaredFields()) { | ||
if (!field.isAccessible()) { | ||
field.setAccessible(true); | ||
} | ||
fields.add(field); | ||
} | ||
return new ObjectParamMetadata(fields); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.