What is Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can „just run“. It take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty, WebLogic(no need to deploy WAR files)
  • Provide opinionated ’starter‘ POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

What you’ll need

I use for this Project Gradle 3.0.

Gradle Configuration

buildscript {

ext {

springBootVersion = '1.5.3.RELEASE'

}

repositories {

mavenCentral()

}

dependencies {

classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

}

}

apply plugin: 'java'

apply plugin: 'eclipse'

apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'

sourceCompatibility = 1.8

repositories {

mavenCentral()

}

dependencies {

compile('org.springframework.boot:spring-boot-starter-data-jpa')

compile('org.springframework.boot:spring-boot-starter-web')

 

compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.0.pr3'

compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.0.0'

compileOnly('org.projectlombok:lombok')

compile group: 'javax.servlet', name: 'jstl', version: '1.2'

compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.0.M21'

compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '1.5.3.RELEASE'

testCompile('org.springframework.boot:spring-boot-starter-test')

compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'

}

The Model

package io.blackground.jobfinder.models;

/**
* @author yotti
*
*/

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
@Entity
public class Job {
@Id
@GeneratedValue
private long id;

private String position;

private String description;
private int minimumExperienceYears;
private long category;

private String minimumDegree;
private int slots;
private String title;
private String image;
private long contrat;
private Date published;

//private transient Company company;

@ManyToOne
private Company company;

}

The Repository

package io.blackground.jobfinder.Repository;


import java.util.ArrayList;
import java.util.List;

import org.springframework.data.repository.CrudRepository;


import io.blackground.jobfinder.models.Job;



/**
 * @author yotti
 *
 */
public interface JobRepository extends CrudRepository<Job, Long> {
	List<Job> findJobsByTitle(String title);
	List<Job> findByTitleContainingIgnoreCase(String title);
	List<Job> findJobsByCompanyCityContainingIgnoreCase(String city);
	List<Job> findJobsByCompanyCityContainingIgnoreCaseAndTitleContainingIgnoreCase(String city,String title);
	List<Job> findTop2ByOrderByIdDesc();
	List<Job> findTop20ByOrderByIdDesc();
	//Job findByLocation(String location);
}

The Service

package io.blackground.jobfinder.services;

import java.util.List;
import java.util.ArrayList;

import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import io.blackground.jobfinder.Repository.JobRepository;
import io.blackground.jobfinder.Repository.PaginatedJobRepository;
import io.blackground.jobfinder.models.Company;
import io.blackground.jobfinder.models.Industry;
import io.blackground.jobfinder.models.Job;
import io.blackground.jobfinder.models.User;

/**
 * @author yotti
 *
 */
@Service
@Transactional
public class JobService {

	private final JobRepository jobRepository;
	@Autowired
	private UserServiceImpl userService;
	@Autowired
	private CompanyService companyService;
	
	private  PaginatedJobRepository paginatedJobRepository;

	/**
	 * @param taskRepository
	 */
	public JobService(JobRepository jobRepository) {
		super();
		this.jobRepository = jobRepository;
	}

	public List<Job> findAll() {
		List<Job> jobs = new ArrayList<>();
		for (Job job : jobRepository.findAll()) {
			jobs.add(job);
		}
		return jobs;

	}

	public List<Job> findAllJobsByTitle(String title) {
		List<Job> jobs = new ArrayList<>();
		for (Job job : jobRepository.findAll()) {
			if (job.getTitle().equals(title)) {
				jobs.add(job);
			}

		}
		return jobs;

	}

	public List<Job> findAllJobsByCity(String city) {
		List<Job> jobs = new ArrayList<>();
		for (Job job : jobRepository.findAll()) {
			if (job.getCompany().getCity().toUpperCase().contains(city.toUpperCase())) {
				jobs.add(job);
			}

		}
		return jobs;

	}

	public List<Job> findAllJobsByCityandTitle(String city, String title) {
		List<Job> jobs = new ArrayList<>();
		for (Job job : jobRepository.findAll()) {
			if (job.getCompany().getCity().toUpperCase().contains(city.toUpperCase())
					&& job.getTitle().toUpperCase().contains(title.toUpperCase())) {
				jobs.add(job);
			}

		}
		return jobs;

	}

	public void save(Job task) {
		jobRepository.save(task);
	}

	public void delete(long id) {
		jobRepository.delete(id);
	}

	public Job findJobById(long id) {
		return jobRepository.findOne(id);
	}

	public List<Job> findJobByCompany() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

		User user = userService.findByUsername(authentication.getName());
		Company userCompany = companyService.findCompany(user);
		List<Job> jobs = new ArrayList<>();
		for (Job job : jobRepository.findAll()) {
			if (job.getCompany().getId() == userCompany.getId()) {
				jobs.add(job);
			}

		}
		return jobs;

	}

	public List<Job> findJobsByTitle(String title) {
		return jobRepository.findJobsByTitle(title);
	}

	public List<Job> findByTitleContainingIgnoreCase(String title) {
		return jobRepository.findByTitleContainingIgnoreCase(title);
	}

	public List<Job> findJobsByCompanyCityContainingIgnoreCase(String city) {
		return jobRepository.findJobsByCompanyCityContainingIgnoreCase(city);
	}
	
	public List<Job> findJobsByCompanyCityContainingIgnoreCaseAndTitleContainingIgnoreCase(String city, String title) {
		return jobRepository.findJobsByCompanyCityContainingIgnoreCaseAndTitleContainingIgnoreCase(city,title);
	}
	public List<Job> findTop2ByOrderByIdDesc(){
		return jobRepository.findTop2ByOrderByIdDesc();
	}
	public List<Job> findTop20ByOrderByIdDesc(){
		return jobRepository.findTop20ByOrderByIdDesc();
	}
	

}

The RestController

/**
 * 
 */
package io.blackground.jobfinder.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.blackground.jobfinder.models.Job;
import io.blackground.jobfinder.services.JobService;
import io.blackground.jobfinder.services.PaginatedJobService;

/**
 * @author yotti
 *
 */

@org.springframework.web.bind.annotation.RestController
@RequestMapping("/meinejobs")
public class RestController {
	@Autowired
	private JobService jobservice;
	@Autowired
	private PaginatedJobService paginatedJobService;

	@RequestMapping(method = RequestMethod.GET)
	public List<Job> getAll() {
		return jobservice.findAll();
	}

	@RequestMapping(method = RequestMethod.POST)
	public Job create(@RequestBody Job contact) {
		return null;
	}

	@RequestMapping(method = RequestMethod.DELETE, value = "{id}")
	public void delete(@PathVariable Long id) {
		jobservice.delete(id);
	}

	@RequestMapping(method = RequestMethod.PUT, value = "{id}")
	public Job update(@PathVariable String id, @RequestBody Job contact) {
		return null;
	}

	

}

The RestController class, is the class that help to create Rest Webservices in Spring Boot.Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default. Click here to learn more.

Demo

The Demo Url for the Rest Services is

https://jobfind-master.herokuapp.com/meinejobs

You can use Postman to test it:

Source code

https://github.com/allipierre/JOBSPRING.git

In the Next Tutorial, we will see, how to consume this Rest Webservices in Spring using as View JSP.