Monthly Archives: September 2018

Spring Boot – Create REST API Basic (Hello World)

Hey guys¬†ūüĎčIn the last tutorial we have been already learn about create new spring boot application. In this tutorial, we will learn how to create REST API basic in spring boot.

Spring Boot – Create REST API Basic (Hello World)

First, create new package or folder with name “controller”.

Second,¬†create new java class with name “HelloController.java”. After that copy and paste source code below to your new class:

@GetMapping(value = "/hello-world")
 public ResponseEntity showHelloToPeople(){

   String hello = "Hello world!";

   return new ResponseEntity<>(hello, HttpStatus.OK);
  }

Third, run your application and open your browser (chrome, opera, safari, IE 11) then goto “http://localhost:8080/spring-boot/hello-world”.

Spring Boot - Create REST API Basic (Hello World)

Voillaa.. you just create a basic REST API.

On the next spring boot tutorial we will learn about how to connect to database MongoDB. Don’t forget, If you have a question, please feel free to¬†share your thoughts in the comments below. Thank you¬†ūüôā

References

Spring Boot – Create New Application

In this tutorial we will try to create new spring boot application.

Note: before starting this tutorial make sure you have installed (Apache Maven, Java 8 and also IntelliJ IDEA for coding editor)

Spring Boot - Create New Application

Spring Boot – Create New Application

To create new spring boot application, please follow this steps below:

  1. Go to spring init site to bootstrap your application here -> start.spring.io
  2. Make sure you fill in all required fields
    • Generate a “Maven
    • With “Java
    • and Spring Boot “2.0.5
    • Project meta data
      • Group “com.cimblo
      • Artifact “helloworld
    • Dependencies
      • Web
  3. Click “Generate Project” to download project archetype
  4. After that open and extract file “helloworld.zip”
  5. Open archetype project with your IDE
  6. Run your application

Screenshoot

Finally, if your first application running with successfully will be like this picture below.

spring boot hello world example

Summary

In this tutorial you have bean learn how to create and run spring boot application. This is just a little step to mastering spring boot application. On the next tutorial we will learn about how to create REST API Basic (Hello World). If you have a question, please feel free to¬†share your thoughts in the comments below. Thank you ūüôā

WebFlux Pagination For Reactive Spring Data

In this example I’m trying to create wrapper for pagination with reactive code in spring 5. I have created simple rest endpoint for finding all entities using spring web-flux and spring data reactive mongoDB with pagination.

WebFlux Pagination For Reactive Spring Data

In Spring Data 1.0, we have PagingAndSortingRepository which inherits from CrudRepository. In reactive Spring Data, we only have ReactiveSortingRepository which inherits from ReactiveCrudRepository. I’m using Reactive Spring Data MongoDB repositories, but this library do not provide paging in the sense of paging how it’s designed for imperative repositories. Imperative paging requires additional details while fetching a page. Then, how apply pagination in reactive Spring Data? or how could we make pagination in a reactive way?

First, I know that the reactive support in Spring Data does not provide means of a Page return type. But, the Pageable parameter is supported in method signatures passing on limit and offset to the drivers and therefore the store itself, returning a Flux that emits the range requested. So you can still fetch chunks of data yourself by passing a Pageable (PageRequest) to repository query methods, for example:

interface ReactivePersonRepository extends Repository<Person, Long> {
 Flux findByFirstnameOrderByLastname(String firstname, Pageable pageable);
}

Spring Data will apply pagination to the query by translating Pageable to LIMIT and OFFSET.

Second, we can use skip and take methods in reactive support. Flux provides skip and take methods to get pagination support, and you also can use filter and sort to filter and sort the result.¬†But that’s not enough for me, this is not the answer!.

Reference for spring official documentation: Reactive repository usage

Third, You can clone or download my example for the WebFlux Pagination For Reactive Spring Data on this git repository.

In this project, I’m using spring boot 2.0, mongoDB for the database and lombok plugin for simplify getter and setter.

WebFlux Pagination For Reactive Spring Data

Example for usage:

  1. On the service class:
    Mono<Page<Basic>> findAll(Pageable pageable);
  2. On the service implementation class:
    @Autowired
      private PageUtils pageUtils;
    
      @Override
      public Mono<Page<Basic>> findAll(Pageable pageable) {
        Query query = new Query();
        query.with(pageable);
        return pageUtils.toPage(query, pageable, Basic.class);
      }

Set Permanent Base PATH Variable on Ubuntu

how to add a directory to the PATH environment variable in linux?

Set Permanent Base PATH Variable on Ubuntu

Open terminal and type command “sudo su”, then edit bashrc file with nano or vim like below:

nano ~/.bashrc

After that copy and paste your export PATH into bashrc file then save and exit.

Summary

The bashrc file will run when you login via the console, or log in using ssh. After that your PATH settings from bashrc will then be available. In contrast, once you are logged in, and you open a command shell or run the bash command.

What Is Spring Boot?

Spring Boot is a framework created by Pivotal. Spring Boot is very popular in java community because this framework can make it easy to create stand-alone web application. This framework is designed to simplify the bootstrapping and development of a new Spring application. Developers just need a very little Spring configuration to run web application, because the framework takes an opinionated approach to configuration and helping developers to speed up configurations.

What Is Spring Boot?

Spring Boot is the most popular framework in the world for Java, Kotlin and Groovy developers (backend developer). Any programmers can make and runs new web application on a web server in just less than five minutes with spring boot.

Spring Boot Main Features

  1. Create stand-alone Spring applications
  2. No more boilerplate configuration
  3. Provide opinionated ‘starter’ dependencies to simplify your build configuration
  4. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  5. Provide production-ready features such as metrics, health checks and externalized configuration
  6. Automatically configure Spring and 3rd party libraries whenever possible
  7. Absolutely no code generation and no requirement for XML configuration

what is spring boot?

Summary

Spring boot can make your life easier because this is a framework which provides RAD (Rapid Application Development). I’m really love with this framework because this spring boot contains lots of magic :). So what is Spring Boot¬†according to you? Please comment in down below if you have another opinion.

References

How to Get Application to Launch at System Startup on Macbook Pro

Sometime i need my apps to launch automatically when i turn on my laptop MacBook Pro (13-inch, 2017, Four Thunderbolt 3 Ports).

To make those apps auto-start upon logging in to your Mac is very easy. You can just use System Preferences to add and remove auto-start items.

Why i want to automatically launch applications when starting my Macbook Pro?

Because every day in my life when I boot¬†my Mac, I always go through the routine of launching the same apps. In my case, Google Chrome, Skype, Slack and IntelliJ IDEA (my favourite¬†Integrated development environment). My other reasons is because I’m to lazy to search and click manually to launching the exact same apps every single day. It doesn‚Äôt make sense to not automate this.

By setting my Mac to automatically launch most used applications, it just saving time. That is the main benefit.

How to Get Application to Launch at System Startup on Macbook Pro?

Next how to set applications to automatically launch at boot up on Macbook Pro?

To make an app launch automatically on your Mac, simply do the following:

Step 1: Open System Preferences.

Macbook System Preferences to automate apps to launch at system startup

Step 2: Click Users & Groups.

How to Get Application to Launch at System Startup on Macbook Pro

Step 3: Click Login Items.

At the bottom left corner of the window, click on the lock icon and enter your admin password.

Step 4:¬†Click the ‚Äė+‚Äė sign and find the Application that you wish to auto-start via the Finder interface. You probably want to open the /Applications folder to do so. You can select multiple items by holding down the¬†‚Ćė key while selecting each item.

Step 5: Once your desired items are selected, click the Add button.

Step 6: Then check the Hide box next to the name of the app under the Login Items tab to run an app when startup in hidden mode.

Verify the presence of the apps that you just added to the list, and reboot; or log out of macOS and log back in. Once you do, you should see the apps that you added automatically launch.

If you‚Äôd like to rollback or stop an app from automatically starting, follow steps 1-3, and highlight the app that you wish to remove and click the ‘-‘ sign to remove the app from login items.

It might be a good idea to think about your workflow when you fire up your machine. If you always find yourself launching the same apps, then why not automate the process? I hope you enjoy this Mac tutorials about “How to Get Application to Launch at System Startup on Macbook Pro”. Finally, let me know what you think down below in the comments (feel free to ask), and sound off with your thoughts on automated app launching.

List of Java Processes On Ubuntu | MAC OS X

This is the simple way to find a java process on linux or mac os x. Sometime if you want “kill” a java process maybe you will like to know the process identifier (PID).

List of Java Processes On Ubuntu | MAC OS X

List of Java Processes On Ubuntu | MAC OS X

ps -ax | grep java
ps aux | grep java
ps -e | grep java
ps -A | grep java
ps -fea | grep -i java
ps axuwww | grep java | grep -v grep

The commands in above will

  • show you all processes with long lines (arg: www)
  • filter (grep) only lines what contain the word java, and
  • filter out the line “grep java

Need more help? go to ubuntu community

Allow Remote Access On MongoDB

By default MongoDB doesn’t allow remote connections. MongoDB bind to local interface only, it will restrict the remote connections, besides that to allow LAN connections from Application Server open and modify your mongod.conf file like below.

In this tutorial i’m using:

Allow Remote Access On MongoDB (Public Access)

$ nano /etc/mongod.conf

# /etc/mongod.conf

# Listen to Any/Public.
bindIp = 0.0.0.0

then restart mongod.conf file to take effect with this command “sudo service mongod restart“. Because if you not restart your new configuration, you will not take any effect on your system.

Allow Remote Access On MongoDB

Allowing IpTables Firewall For Public Access

Finally, allow connections on port 27017 to your firewall.

iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

Summary

In this tutorial, you have learned how to enable remote access to a MongoDB server. For another mongoDB tutorial you can learn from official documentation. Also do not forget to learn how to use mongoDB in real world programming with spring data mongoDB for inserting, updating and deleting data in mongoDB.

References

Spring Data MongoDB – Upsert

In this tutorial i will share to you how to use UpSert operation in spring data mongoDB.

If no document that matches the query, a new document is created by combining the query and update object.

@Autowired
private MongoTemplate mongoTemplate;

//search a document that doesn't exist
Query query = new Query();
query.addCriteria(Criteria.where("id").is("cimblo"));
Update update = new Update();
update.set("age", 21);
mongoTemplate.upsert(query, update, User.class);

Spring Data MongoDB – Upsert

The output is a new document created by combining both query and update object.

Spring Data MongoDB

For more reference mongoDB you can follow this link below: