Tag Archives: coding

Spring @RestController vs @Controller

To understand the difference between @RestController and @Controller we can think about the main difference between a REST API and a Web application. The response of a REST API is generally JSON or XML; in a web application, instead, the response is usually a view (some html + css) intended for human viewers.

This is also the main difference between @Controller and @RestController annotation. Basically in a spring mvc @Controller the handler method returns the response “view name” which is resolved to a view technology file (e.g. JSP) and the parsed view content is sent back to browser client. It just create a map of the model object and find a view. If we want instead to bundle the return value yo a web response body we can use the @ResponseBody annotation and no view resolver is needed.

For example assuming that we have a view and a greeting Api in a MVCController that take a param as input and return a view we can use @Controller annotation

// Path of view template

public class MVCController {

    public String greeting(@RequestParam(name = "name", required = 
              false, defaultValue = "World") String name, Model model) {
        model.addAttribute("name", name);
        // We can return a view name that is present in 
        return "greeting";

The @RestController simply returns the object and object data is directly written into HTTP response as JSON or XML. One notice is that in Spring MVC the @RestController annotation is a combination of @Controller and @ResponseBody annotation.

public class MVCController {
  // your logic

public class RestFulController { 
  // your logic

List to int[] in Java

How to convert a List<Integer> to a int[] in Java 8? Using Java 8 collections stream() function and then mapping to ints, we get an IntStream. With the IntStream we can call toArray() which gives us the int[]

public int[] toIntArray(List<Integer> list){
   return list.stream().mapToInt(Integer::intValue).toArray();

From bash to zsh…git and prompt configuration

After a really long journey I decided to retire my long-lasting and trustworthy companion, my 2012 MacBook Air. I spent quite some time to decide which new model to buy; in the end the presence of the touch bar made me avoid all other models and going for the latest 2020 MacBook Air.

I didn’t want to use one of my time capsule backup but have a new clean env to setup and so first thing first: git, bash-completion and customization of the prompt on zsh shell on macOs Catalina. I used brew to do it.

brew install git bash-completion

And then to make the completion available in zsh as suggested by homebrew website you must get the Homebrew-managed zsh site-functions on your FPATH before initialising zsh’s completion facility, so I added the following in the ~/.zshrc file:

if type brew &>/dev/null; then
  FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
  autoload -Uz compinit && compinit

Compinit was complaining about insecure directories, so I listed the insecure directory and added the correct permission for those.

zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]? 


This is because those folders are group writable, so I changes it:

compaudit | xargs chmod g-w

I do like do see in my prompt both the user and the machine name, the path I’m on and if it’s a git repository the branch name and adding some color. Next step will be adding some more icon to show the status of the git repository.

# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats '(%b)'

autoload -U colors && colors

# Set up the prompt (with git branch name)
PROMPT='%n@%m %F{green}${PWD/#$HOME/~}%f %F{yellow}${vcs_info_msg_0_}%f > '
zsh custom prompt
work in progress of my zsh prompt customization

Destroy vs Delete in Rails

It has been not really clear in my mind the difference between “destroy” and “delete” for a while before attempting to delete an object and its related once. In this situation and, reading carefully the documentation and the source code, I have discovered that was impossible since delete is used to delete the objects without instantiating it and executing the callbacks, including :dependent for associations. While using destroy the object is instantiated first and so callbacks and filters are triggered.

Assuming the existence of two model a Customer and an Address. With a relation of has_many that connects them:

class Customer < ActiveRecord::Base
has_many :addresses, dependent: :destroy
class Address < ActiveRecord::Base
belongs_to :customer

I’ve tried to use delete to remove the customer from the database but all the addresses were pending in the database orphans. Then I’ve used “destroy” to destroy the customer and all the addresses related.


In this way the customer and all the addresses (marked using “dependent” option) are deleted.

Ruby and its destructive methods…

In ruby there are methods followed by a “!” symbol (exclamation mark). These methods alter the object itself.

For example in the Array class there are two versions of shuffle method
shuffle and shuffle!.

a = [1,2,3, 4] => [1, 2, 3, 4]
a.object_id => 70279519801380
a.shuffle.object_id => 70279519867040
a.shuffle!.object_id => 70279519801380

  • Shuffle version returns a new array with elements of self shuffled and as a.shuffle.object_id shows a different value from a.object_id.
  • shuffle! modifies the object itself,  shuffling elements in self in place and a.shuffle!.object_id shows the same object_id of a.object_id.