Socket
Book a DemoInstallSign in
Socket

com.devskiller.friendly-id:friendly-id-project

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

com.devskiller.friendly-id:friendly-id-project

Library to convert uuid to url friendly IDs basing on base62

Source
mavenMaven
Metadata Only
Version
1.1.0
Version published
Maintainers
1
Source

Build Status Maven Central

friendly ID

Library to convert UUID to url friendly IDs basing on Base62

UUID                                        Friendly ID

c3587ec5-0976-497f-8374-61e0c2ea3da5   ->   7NLCAyd6sKR7kDHxgAWFPG
|                                           |                              
36 characters                               22 characters

                              

Spring Boot integration

Just add friendly-id starter dependency:

<dependency>
    <groupId>com.devskiller.friendly-id</groupId>
    <artifactId>friendly-id-spring-boot-starter</artifactId>
    <version>1.0.4</version>
</dependency>

Sample application:

@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @GetMapping("/bars/{bar}")
    public Bar getBar(@PathVariable UUID bar) {
        return new Bar(UUID.randomUUID());
    }

    @Value
    class Bar {
        private final UUID id;
    }
}  

Result:

curl 'localhost:8080/bars/5fD1KwsxRcGhBqWNju0jzt' 
{"id":"52OMXhWiAqUWwII0c97Svl"}

Application uses internally UUID, but from external point of view only friendly id is visible.

Testing improvments

This allows to write much simpler tests for our code, because you can use names instead of hard to remember UUIDs.

mockMvc.perform(get("/bars/{bar}", "sampleBar"))
  .andExpect(status().isOk())
  .andExpect(jsonPath("$._links.self.href", is("http://localhost/bars/sampleBar")));

There is no need to define UUID value for bar, sampleBar is decoded to valid UUID identifier.

FriendlyID library

Dependency:

<dependency>
    <groupId>com.devskiller.friendly-id</groupId>
    <artifactId>friendly-id</artifactId>
    <version>1.0.4</version>
</dependency>

Usage

FriendlyId.createFriendlyId();
// 7NLCAyd6sKR7kDHxgAWFPG

FriendlyId.toUuid("7NLCAyd6sKR7kDHxgAWFPG");
// c3587ec5-0976-497f-8374-61e0c2ea3da5

FriendlyId.toFriendlyId(UUID.fromString("c3587ec5-0976-497f-8374-61e0c2ea3da5"));
// 7NLCAyd6sKR7kDHxgAWFPG

Note

  • Id 00cafe is equal to cafe - leading zeros are ignored.

  • UUID is a 128-bit number, so id also can store only 128-bit number

Jackson integration

Add jackson module dependency:

<dependency>
    <groupId>com.devskiller.friendly-id</groupId>
    <artifactId>friendly-id-jackson-datatype</artifactId>
    <version>1.0.4</version>
</dependency>

Register friendly_id module:

ObjectMapper mapper = new ObjectMapper()
   .registerModule(new FriendlyIdModule());

FAQs

Package last updated on 04 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts