Comments (11)
What time can use @ModelAttribute
?
from spring-cloud-openfeign.
If someone needs quick workaround for that -- we used custom Encoder in our feign client context:
@Bean
public Encoder encoder() {
Encoder.Default defaultEncoder = new Encoder.Default();
return new Encoder() {
@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if(bodyType == MyCustomParam.class) {
encodeCustomParam((MyCustomParam) object, template);
} else {
defaultEncoder.encode(object, bodyType, template);
}
}
private void encodeCustomParam(MyCustomParam object, RequestTemplate template) {
MyCustomParam param = ((MyCustomParam) object);
template.query("var1", String.valueOf(param.var1));
template.query("var2", String.valueOf(param.var2));
}
};
}
and method in feign client:
@RequestMapping(method = GET, value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
MyCustomResponse getByCustomParam(MyCustomParam param);
from spring-cloud-openfeign.
Looking at line 129 of org.springframework.cloud.netflix.feign.support.SpringMvcContract I see
// TODO: support spring parameter annotations?
So the answer appears to be no at the moment
from spring-cloud-openfeign.
Would be nie to have that
Nick Smith [email protected] schrieb am Do., 22. Okt. 2015 3:19 PM:
Looking at line 129 of org.springframework.cloud.netflix.feign.support.SpringMvcContract
I see// TODO: support spring parameter annotations?
So the answer appears to be no at the moment
—
Reply to this email directly or view it on GitHub
https://github.com/spring-cloud/spring-cloud-netflix/issues/609#issuecomment-150221269
.
from spring-cloud-openfeign.
I'll fork and try to fix
from spring-cloud-openfeign.
@nickcodefresh will your fix include support for @ModelAttribute (as described in #617)?
Thanks
from spring-cloud-openfeign.
To make @Aloren 's answer more generic, one can use reflection and annotation-processing
@Bean
public Encoder encoder() {
Encoder defaultEncoder = new Encoder.Default();
return (object, bodyType, template) -> {
Class clazz= (Class)bodyType;
if (clazz.getAnnotation(MyModelAttributes.class) != null) {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
try {
Object data = field.get(object);
if(data == null) continue;
if(field.getType().getName().equals("java.util.List")){
List<String> s = new ArrayList<>();
for(Object o : (List<?>) data){
s.add(o.toString());
}
template.query(field.getName(), s.toArray(new String[]{}));
} else {
template.query(field.getName(), data.toString());
}
} catch (IllegalAccessException e) {
continue;
}
}
} else {
defaultEncoder.encode(object, bodyType, template);
}
};
}
and put @MyModelAttributes
on MyCustomParam class.
from spring-cloud-openfeign.
Mark. I want to use @ModelAttribute too.
from spring-cloud-openfeign.
maybe you need this. https://stackoverflow.com/questions/35621062/how-to-custom-feignclient-expander-to-convert-param/35835085?noredirect=1#comment86877708_35835085
from spring-cloud-openfeign.
Thank you for your provide.I make a @JsonArguments in Feign but @ModelAttribute is not useful in controller. Can you give me some advice?
from spring-cloud-openfeign.
@Dreampie Hi, I code a @JsonArguments like your provide.
The url is : http://192.168.31.140:8081/api/v1/asset/repay-plan/page?request=%7B%22pageNum%22%3A1%2C%22pageSize%22%3A15%2C%22loanInvoiceId%22%3A%22111%22%7D
But it is can't be use in controller.
Could you please provide a full code which I can learn?
from spring-cloud-openfeign.
Related Issues (20)
- PlaytikaOSS feign-reactive does not support spring boot 3.2.x HOT 1
- @PatchMapping and @DeleteMapping insert unwanted slash when using url and specific value HOT 3
- FeignClientFactoryBean dismiss404 default config overrides client-specific config HOT 1
- Multiple implementations of ResponseInterceptor fail to trigger HOT 10
- Want to understand , is Spring Openfeign is in Deprecated path HOT 1
- Maintain the span from HTTP calls using feign clients with micrometer HOT 1
- Micrometer tracing propagation not working when using Spring FactoryBean and Feign HOT 1
- Custom HTTP codes are not supported HOT 1
- B3 span and parentId headers not propagated with spring-cloud-openfeign and brave HOT 1
- Remove unused spring-boot-starter-aop dependency HOT 1
- [sentinel] openfeign+sentinel Error HOT 2
- Spring Boot 3.2.4 (Cloud 2023.0.1) Cannot deserialize "pageable": "INSTANCE" HOT 5
- Issue with registering method parameter hints HOT 3
- `FeignClientsRegistar` fails when trying to parse unresolved property placeholders in JDK 20+ HOT 1
- "text/plain" Not working when dealing with the ISO_8859_1 HOT 1
- feign.codec.EncodeException: null HOT 3
- RequestInterceptor not injecting headers after upgrading it to 2023.0.1
- Default http client (Client.Default) seems not taking 'spring.cloud.openfeign.httpclient' properties when creating feign client with FeignClientBuilder HOT 1
- FeignClients auto config
- Unable to read the totalElements property of Page from Spring Data Commons 3.3.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-cloud-openfeign.