com.klaussoft.springtest.CostumerController.java Source code

Java tutorial

Introduction

Here is the source code for com.klaussoft.springtest.CostumerController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.klaussoft.springtest;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Claudio
 */
@RestController
public class CostumerController {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String args[]) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    //Method from spring tutorial
    @RequestMapping("/costumer")
    public Costumer costumer(@RequestParam(value = "name", defaultValue = "World") String cname) {

        log.info("Creating tables");

        jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
        jdbcTemplate
                .execute("CREATE TABLE customers(" + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");

        // Split up the array of whole names into an array of first/last names
        List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream()
                .map(name -> name.split(" ")).collect(Collectors.toList());

        // Use a Java 8 stream to print out each tuple of the list
        splitUpNames
                .forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1])));

        // Uses JdbcTemplate's batchUpdate operation to bulk load data
        jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames);

        log.info("Querying for customer records where first_name = 'Josh':");
        jdbcTemplate
                .query("SELECT id, first_name, last_name FROM customers WHERE first_name = ?",
                        new Object[] { "Josh" }, (rs, rowNum) -> new Costumer(rs.getLong("id"),
                                rs.getString("first_name"), rs.getString("last_name")))
                .forEach(customer -> log.info(customer.toString()));

        return null;
    }

    //my method:
    @RequestMapping("/costumerme")
    public List<Costumer> costumerme(@RequestParam(value = "name", defaultValue = "World") String cname) {

        List<Costumer> costumers = new ArrayList<Costumer>();

        log.info("Creating tables");

        jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
        jdbcTemplate
                .execute("CREATE TABLE customers(" + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");

        // Split up the array of whole names into an array of first/last names
        List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream()
                .map(name -> name.split(" ")).collect(Collectors.toList());

        // Use a Java 8 stream to print out each tuple of the list
        splitUpNames
                .forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1])));

        // Uses JdbcTemplate's batchUpdate operation to bulk load data
        jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames);

        log.info("Querying for customer records where first_name = 'Josh':");
        jdbcTemplate
                .query("SELECT id, first_name, last_name FROM customers WHERE first_name = ?",
                        new Object[] { "Josh" },
                        (rs, rowNum) -> new Costumer(rs.getLong("id"), "Josh", rs.getString("last_name")))
                .forEach(customer -> costumers.add(customer));

        return costumers;
    }

}