How to resolve slashes in path variable in Spring MVC

Today I stumbled across a problem where a slash can occur in a path variable.

The contents of the path variable can look like this:
1/53879d5c-b07b-44f2-9a77-b99f67bb8481 or even 1/2/53879d5c-b07b-44f2-9a77-b99f67bb8481

At the backend, I need the full path in a variable because the path can contain several slashes but does not have to.

Let’s have a look at this as a code example to make it clear.

@GetMapping("/rest/{path}")
public @ResponseBody ResponseEntity<Container> getContainerByPath(@PathVariable String path) {

  return containerService.getByPath(path);

}

The path /1/53879d5c-b07b-44f2-9a77-b99f67bb8481 will not work.

The solution is HttpServletRequest.

// /rest/1/53879d5c-b07b-44f2-9a77-b99f67bb8481
@GetMapping("/rest/**")
public @ResponseBody ResponseEntity<Container> getContainerByPath(HttpServletRequest request) {

  String path = extractPath(request);

  return containerService.getByPath(path);

}
private String extractPath(HttpServletRequest request) {

  String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
  String matchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); // /rest/**

  return new AntPathMatcher().extractPathWithinPattern(matchPattern, path); // 1/53879d5c-b07b-44f2-9a77-b99f67bb8481
}

 

 

 

How To Organize Your Day With The Ivy Lee Method

The Ivy Lee method is a 100-year-old strategy to help people become more productive at work.

The Ivy Lee method is simple and effective. Just follow these five steps:

  • At the end of each workday, write down the six most important things you need to accomplish tomorrow. Do not write down more than six tasks.
  • Prioritise those six items in order of their real importance.
  • When you start a new day tomorrow, concentrate only on the first task. Work until the first task is finished before moving on to the second task.
  • Approach the rest of your list in the same fashion. At the end of the day, move any unfinished items to a new list of six tasks for the following day.
  • Repeat this process every (working) day.

HATEOAS And HAL – And Why You Should Use It For Your REST API

Let’s face it, we all love REST. As data models grow and functionality increases, REST APIs can quickly become complex.

For the design and documentation of an API, I use Swagger.

In the screenshot, you can see an example API as shown by the Swagger UI.

There is no logical link between individual resources.

 

HATEOAS/HAL extends REST by the possibility to guide a user/client through.

HATEOAS is a concept of application architecture. It defines the way in which a clients application interact with the server, by navigating hypermedia links they find inside resource models returned by the server.

Since HATEOAS is only a concept, we need some standard to describe the resources, that contain hypermedia information (links to related resources).
HAL is one of such standards. It is a specific format of resource presentation, that can be used to implement HATEOAS.

For example, an answer without HATEOAS might look like this:

{
  "id": 1,
  "city:" "Vienna"
}

A client without knowledge of the possibilities of the API does not know now that he may change or delete the entry.

Let’s assume you want to tell the client that it can edit and delete this entry. With REST this is not possible.

Let’s look at the same example with implementing HATEOAS using HAL to describe the links:

URI: http://localhost:8080/api/v1/cities/1

{
  "id": 1,
  "city": "Vienna",
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/v1/cities/1"
   }
 }
}

The first link assigned to a resource is called a self-link. It is the self-relationship – the canonical place where the resource is accessible.

This link is of course not very helpful. Let’s have a look at a more complex example: a client should change the entry “Vienna” and confirm afterwards.

The first case is simple, and CRUD provides us with the operation PUT, which usually refers to the self-link. However, how do we determine the confirmation link?
We use an additional link which we label with the label “accept”.

The client that implements the API has to check in the list of links if there is a function “accept” and follow the link.

URI: http://localhost:8080/api/v1/cities/1

{
  "id": 1,
  "city": "Vienna",
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/v1/cities/1"
    },
    "accept": {
      "href": "http://localhost:8080/api/v1/cities/1"
    }
  }
}

 

Do You Know The Difference Between Plugins Vs. Themes?

Do you know what the difference is between a WordPress theme and a WordPress plugin? In the case of a website stops working, it’s important to understand that.

Sometimes a website stops working overnight. It could happen if you use an outdated theme or plugins.

Every minute you lose visitors…

In this case, it’s important to fix the issue quickly.

Switching the theme isn’t an option, because there are hundreds of different content elements, content builder and so on.
It took hours, if not weeks to recreate the content with another theme.

This is why you need to understand the basics of WordPress and the difference between Plugins and Themes.

So let’s start with themes.

What Is a Theme?

If we were to compare a WordPress website to a building, the theme is like the building’s exterior.
Simply said, the colours, shape, layouts, etc.

A WordPress Theme controls the visible part of your website. What the visitor sees and interacts with. The Theme takes all the information that is stored on your site (pages, posts, sidebars, navigation, etc.) and determines how to organise and display it.

For example, if we have a blog post, the theme will control the look of the post:

Should there be an author name?

What font size should the title be?

If you want to get deeper into this, please refer to the WordPress Theme Development Codex.

What Is a Plugin?

Let me return to the house metaphor, if the theme is like the exterior of the building, think of plugins as the interior or building equipment: Heating, cooling, lighting.

Plugins are extensions of WordPress. They extend the core with new functionality or remove some.

For example Ninja Forms, which let you build forms within minutes using a simple yet powerful drag-and-drop form creator. WordPress has no feature like this.

What’s coming next?

In part 2 I will go deeper into plugins and themes.

 

Sign up for my newsletter to keep updated.

Tools and Apps I Use Every Day as a Web Developer

Every web developer has its environment, and we use so many different tools for our day-to-day workflow.

As I wrote this article, I found it very frightening how many tools I use or need. We can quickly forget about everything we depend on for our work.

Tools help us to organise and ease our daily work.

Here are my favourites.

Productivity Tools

  • Grammarly
  • Toggl
  • Google Docs
  • Google Keep

Communication Tools

  • WhatsApp Desktop
  • Telegram Desktop
  • Slack
  • Mattermost

Development and Workflow Tools

  • CMDer
  • GitHub Desktop
  • DBeaver
  • Vagrant and Docker
  • Visual Studio Code
  • PHPStorm
  • WebStorm
  • CodePen
  • Angular CLI

Google Chrome with the following apps installed

  • ColorZilla
  • JSON Formatter
  • User-Agent Switcher
  • Augury
  • Chrome Remote Desktop
  • Momentum

Books You Need To Read in 2018

“Don’t Read Books – Use Books” – Dan Lok

As an entrepreneur, it’s important to be ahead of others. The best source for knowledge are books.
To gain the knowledge you have to read every day.
Read for 30 to 60 minutes a day, will take you 80% further than your competitors.

KNOWLEDGE is the KEY TO SUCCESS!

Here are my top 4 books you definitely have to read right now.