Package feign.codec

Interface Encoder

  • All Known Implementing Classes:
    Encoder.Default

    public interface Encoder
    Encodes an object into an HTTP request body. Like javax.websocket.Encoder. Encoder is used when a method parameter has no @Param annotation. For example:

     @POST
     @Path("/")
     void create(User user);
     
    Example implementation:

     public class GsonEncoder implements Encoder {
       private final Gson gson;
    
       public GsonEncoder(Gson gson) {
         this.gson = gson;
       }
    
       @Override
       public void encode(Object object, Type bodyType, RequestTemplate template) {
         template.body(gson.toJson(object, bodyType));
       }
     }
     

    Form encoding

    If any parameters are found in MethodMetadata.formParams(), they will be collected and passed to the Encoder as a map.

    Ex. The following is a form. Notice the parameters aren't consumed in the request line. A map including "username" and "password" keys will passed to the encoder, and the body type will be MAP_STRING_WILDCARD.

     @RequestLine("POST /")
     Session login(@Param("username") String username, @Param("password") String password);
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Encoder.Default
      Default implementation of Encoder.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.reflect.Type MAP_STRING_WILDCARD
      Type literal for Map<String, ?>, indicating the object to encode is a form.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void encode​(java.lang.Object object, java.lang.reflect.Type bodyType, RequestTemplate template)
      Converts objects to an appropriate representation in the template.
    • Field Detail

      • MAP_STRING_WILDCARD

        static final java.lang.reflect.Type MAP_STRING_WILDCARD
        Type literal for Map<String, ?>, indicating the object to encode is a form.
    • Method Detail

      • encode

        void encode​(java.lang.Object object,
                    java.lang.reflect.Type bodyType,
                    RequestTemplate template)
             throws EncodeException
        Converts objects to an appropriate representation in the template.
        Parameters:
        object - what to encode as the request body.
        bodyType - the type the object should be encoded as. MAP_STRING_WILDCARD indicates form encoding.
        template - the request template to populate.
        Throws:
        EncodeException - when encoding failed due to a checked exception.