Coder Social home page Coder Social logo

Comments (11)

Dreampie avatar Dreampie commented on June 4, 2024 8

What time can use @ModelAttribute?

from spring-cloud-openfeign.

Aloren avatar Aloren commented on June 4, 2024 2

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.

nickcodefresh avatar nickcodefresh commented on June 4, 2024

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.

cforce avatar cforce commented on June 4, 2024

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.

nickcodefresh avatar nickcodefresh commented on June 4, 2024

I'll fork and try to fix

from spring-cloud-openfeign.

mperrault2 avatar mperrault2 commented on June 4, 2024

@nickcodefresh will your fix include support for @ModelAttribute (as described in #617)?
Thanks

from spring-cloud-openfeign.

sify21 avatar sify21 commented on June 4, 2024

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.

littlefisher666 avatar littlefisher666 commented on June 4, 2024

Mark. I want to use @ModelAttribute too.

from spring-cloud-openfeign.

Dreampie avatar Dreampie commented on June 4, 2024

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.

littlefisher666 avatar littlefisher666 commented on June 4, 2024

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.

littlefisher666 avatar littlefisher666 commented on June 4, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.