Class EndpointTransferMapper


  • public class EndpointTransferMapper
    extends Object
    Defines mappings for certain endpoint types to corresponding transfer types. This allows using beans / classes in endpoints which cannot be serialized by the standard bean serialization.

    Each endpoint parameter value coming from the client is passed through a registered mapper for that endpoint type before the values are passed to the endpoint methods.

    Each endpoint return value is passed through a registered mapper before the value is serialized by the normal serialization mechanism and then sent to the client.

    The endpoint TypeScript is generated based on the mapped types.

    For example, a method like

    public Page<Person> list(Pageable pageable) { }

    generates a TypeScript and converts data as if the method was defined as

    public List<Person> list(dev.hilla.mappedtypes.Pageable pageable) { }

    • Constructor Detail

      • EndpointTransferMapper

        public EndpointTransferMapper()
        Creates a new instance.
    • Method Detail

      • getTransferType

        public Class<?> getTransferType​(Class<?> endpointType)
        Gets the transfer type for the given endpoint type.

        NOTE that this is intended for checking the type of a value being sent at runtime and thus checks also the super types / interfaces of the given type.

        The returned transfer type is the same as the endpoint type if no conversion is needed.

        Parameters:
        endpointType - the endpoint type
        Returns:
        the transfer type or null if no mapping exists
      • getTransferType

        public String getTransferType​(String endpointType)
        Gets the transfer type for the given endpoint type.

        NOTE that this is intended to be used when generating TypeScript and it DOES NOT check super types / interfaces of the given type.

        The returned transfer type is the same as the endpoint type if no conversion is needed.

        Parameters:
        endpointType - the endpoint type
        Returns:
        the transfer type or null if no mapping exists
      • getMapper

        public <T> EndpointTransferMapper.Mapper getMapper​(Class<T> endpointType)
        Gets the mapper for the given endpoint type.

        NOTE that this is intended for runtime and thus checks also the super types / interfaces of the given type.

        Type Parameters:
        T - the endpoint type
        Parameters:
        endpointType - the endpoint type
        Returns:
        the transfer type or null if no mapper exists
      • toTransferType

        public Object toTransferType​(Object endpointValue)
        Converts the given object from its endpoint type to its transfer type.
        Parameters:
        endpointValue - the value returned from the endpoint
        Returns:
        the value converted to its transfer type
      • toEndpointType

        public <T> T toEndpointType​(Object transferValue,
                                    Class<T> endpointType)
        Converts the given object from its transfer type to the given endpoint type.
        Type Parameters:
        T - the endpoint type
        Parameters:
        transferValue - the value transferred over the network
        endpointType - the value type declared in the endpoint, as parameter or return type
        Returns:
        the value converted to its endpoint type