Author Archives: Tiziana Sellitto

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
/src/main/resources/templates/greeting.html

@Controller
public class MVCController {

    @RequestMapping("/greeting")
    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.

@Controller
@ResponseBody
public class MVCController {
  // your logic
}

@RestController
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
fi

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]? 

compaudit

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)
setopt PROMPT_SUBST
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

Chiacchierino, tatting, frivolité…

“Handmade” is something that has been part of me since I have been a little girl, almost written in my DNA.

My grandmother used to spend long afternoons with a needle in her hand sitting at her old Singer sewing machine; My mom was always ready to knit slippers or wool scarfs; She had paper, cardboard, scratch paper, scissors, colors ready for little homemade works … together we used to prepare all the homes and pieces for the Nativity (Presepe) with recycled boxes and acrylic painter.

My sisters and I started with the cross-stitch when my grandmother gave us as gifts linen towels to embroider for our dowry. From that first cross-stitch work, we continued on with the crochet, the knitting and the wood, the paper, the Das … also passing by the kitchen with pasta and pizza … even the belly wants its part 🙂

Each period of my life has had a bit of a “season”. There was crochet, origami, quilling, knitting, recycled paper, sewing (although I still need a lot of work there 😀 ) often all together. Something that has always made me a little bit afraid was tatting, chiacchierino, frivolité depending on which language you want to use.

In the last year I started experimenting with this old art with results not always up to the standards (too high) that I have always fixed on me.

Tatting lavender sachet bag

I started with small things. Small bags of lavender, a sachet bags to put in the drawers. They sums the things I love to do and something new: Tatting… At the moment I have completed the small bags and they smell good 🙂

lavender sachet bag, tatting and cross stitch

Lavender sachet bag, tatting and cross stitch

Lizbeth thread size 40

Lizbeth thread size 40

In this period of extreme hot weather thinking about Christmas makes me feel better. I decided to try a new colored thread and I chose a new brand: Lizbeth.

I searched and found a very nice and professional Etsy shop CustomCottonEtc and I started to order my supply of cotton. My cart is already overwhelmed for the next order 🙂

Tatting snowflake Lizbeth thread size 40

Tatting snowflake Lizbeth thread size 40

With these colors I have in mind so many projects. First of all: Snowflakes!!!!  I have already started with a whole series of different size of snowflake.

They will be a good Christmas Tree Ornament and then…I’m already thinking about other projects…maybe something to practice my sewing skill 🙂

Tatting snowflake Lizbeth thread size 40

Tatting snowflake Lizbeth thread size 40

3d printed home gadgets.

After trial and errors I’ve finally managed to print some small gadgets with my micro 3d printer. Personalized gadget very useful to use at home.

When I bought the printer I choose to buy two spool (ABS white and blue). But since I started printing I found it very difficult to let the material stick to the surface of the printer. I tried printing with support material, with different temperatures, adding scotch tape at the surface, but nothing worked for me. After searching on the web I found out that without a warm bed it’s very difficult working with ABS and so I decided to buy a PLA spool that I use with externally. I set the temperature at 215 degree and with a tape on the surface.

3d printed cell holder

3d printed cell holder

The first object I printed was a phone holder. I printed with low quality and from a design I downloaded from the web. As soon as I realized the printer worked well I started with a personal design.

desk holder

Desk holder

Desk holder

Desk holder

This is a desk holder I printed in medium quality. It seems to work well even if it’s not so strong. I’ve adjusted the printing setting to high quality and high density. With these settings the printer  use more filament, the object weights more but is more strong.

For example this is the door hanger I printed in high quality and density. It took 7/8 hours to complete but it’s really good, strong and resistant.

Door hanger.

Door hanger.

Finished door hanger

Finished door hanger

So. I’m having fun with material, temperature, design of small objects. The next step will be trying with a different design. It’s really early but my mind is already thinking about Christmas 😀

 

Magic in the box

I’ve been waiting for a long time before have been able to open that little box that was parked in the lumber room; a small orange cube that creates magic things was inside of it. The little cube I’m talking about is my new micro 3d printer.

I had never printed anything in my life but I’ve always been fascinated by 3d models. I never really put myself in doing something until one day I designed a 3d coffee cup and I had it printed.

coffee cup 3d model

Coffee Cup 3d Model

In that period I saw a kickstarter campaign and decided to give it a try. It took a long time before the campaign would end and then before the printer was shipped and then used 🙂 But I finally managed to print something.

m3d printing arrangement

m3d printing arrangements

 Actually I’m still in the process of test printing, discover materials, temperature, quality settings, and spool arrangements, calibration. In the meanwhile I’ll be waiting for the next 8 hours for my medium quality personal 3d over door hanger to be printed, designing a 3d door key hanger. I need a lot of useful tools in my house 🙂

And anyway this pla blue spool is much better of my abs white 😉

Outreach Program For Women a year later

It’s passed a year and a new Summer will begin…a new summer for the women that will be chosen and that will start soon the GNOME’s Outreach Program for Women.

This summer Mozilla will participate with three different projects listed here and among them the Mozilla Bug Wrangler for Desktop QA that is the one I applied for last year. It has been a great experience for me and I want to wish good luck to everyone who submitted the application.

I hope you’ll have a wonderful and productive summer 🙂

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
end
class Address < ActiveRecord::Base
belongs_to :customer
end

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.

Customer.destroy(params[:id])

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.