Spring Boot example
application.properties:
# ===============================
# H2 CONSOLE
# ===============================
spring.h2.console.path=/h2
# To See H2 Console in Browser:
# http://localhost:8080/h2
# Enabling H2 Console
spring.h2.console.enabled=true
# ===============================
# DB
# ===============================
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
# ===============================
# JPA / HIBERNATE
# ===============================
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.format_sql=false
-------------------
package eu.microwebservices.awesomeappproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class AwesomeApp {
public static void main(String[] args) {
SpringApplication.run(AwesomeApp.class, args);
}
}
package eu.microwebservices.awesomeappproject.model;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "tab_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* Sorry but, there is no validation here with email!
* It is not secure because of no validation from user input! See https://www.owasp.org
* It is only for educational purposes...
*/
@NotNull
private String email;
/**
* This is a name generally, which could be the nickname or the firstname,
* but if the user prefer it could be last name...
* There is no validation here!
* It is only for educational purposes...
*/
@NotNull
private String name;
public User() {
}
public User(Long id) {
this.id = id;
}
public User(String email, String name) {
this.email = email;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email=" + email +
'}';
}
}
package eu.microwebservices.awesomeappproject.model;
import org.springframework.data.repository.*;
import org.springframework.transaction.annotation.*;
@Transactional
public interface UserDao extends CrudRepository
/**
* This method will find an User instance in the database by its email.
* Note that this method is not implemented and its working code will be
* automagically generated from its signature by Spring Data JPA.
*/
public User findByEmail(String email);
/**
* This method will find an User instance in the database by its name.
* Note that this method is not implemented and its working code will be
* automagically generated from its signature by Spring Data JPA.
*/
public User findByName(String name);
}
package eu.microwebservices.awesomeappproject.controller;
import eu.microwebservices.awesomeappproject.model.User;
import eu.microwebservices.awesomeappproject.model.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
/**
* HOW TO TEST:
* $ mvn spring-boot:run
* http://localhost:8080/
* Use the following urls:
* /create-user?email=[email]&name=[name]: create a new user with an auto-generated id and email and name as passed values.
* /delete-user?id=[id]: delete the user with the passed id.
* /get-user-by-email?email=[email]: retrieve the id for the user with the passed email address.
* /update-user?id=[id]&email=[email]&name=[name]: update the email and the name for the user indentified by the passed id.
*/
@Autowired
private UserDao userDao;
/**
* /create-user --> Create a new user and save it in the database.
* It is not secure operation here! There is no validation here! See https://www.owasp.org
* It is only for REST educational purposes...
*
* @param email User's email
* @param name User's name
* @return A string describing if the user is successfully created or not.
*/
@RequestMapping("/create-user")
@ResponseBody
public String create(String email, String name) {
User user = null;
try {
user = new User(email, name);
userDao.save(user);
}
catch (Exception ex) {
return "Error while creating the user: " + ex.toString();
}
return "User created succesfully!! (id = " + user.getId() + ")";
}
/**
* /delete-user --> Delete the user having the passed id.
* It is not secure operation here! There is no validation here!
* It is only for REST educational purposes...
*
* @param id The id of the user to delete
* @return A string describing if the user is successfully deleted or not.
*/
@RequestMapping("/delete-user")
@ResponseBody
public String delete(Long id) {
try {
User user = new User(id);
userDao.delete(user);
}
catch (Exception ex) {
return "Error while deleting the user: " + ex.toString();
}
return "User deleted successfully!!";
}
/**
* /get-user-by-email --> Return the id for the user having the passed email.
* It is not secure operation here! There is no validation here!
* It is only for REST educational purposes...
*
* @param email The email to search in the database.
* @return The user id or a message error if the user is not found.
*/
@RequestMapping("/get-user-by-email")
@ResponseBody
public String getByEmail(String email) {
Long userId;
try {
User user = userDao.findByEmail(email);
if (user != null) {
userId = user.getId();
} else {
return "User not found!!";
}
}
catch (Exception ex) {
return "User not found!!";
}
return "The user is found, and id is: " + userId;
}
/**
* /update-user --> Update the email and the name for the user in the database
* having the passed id.
* It is not secure operation here! There is no validation here!
* It is only for REST educational purposes...
*
* @param id The id for the user to update.
* @param email The new email.
* @param name The new name.
* @return A string describing if the user is successfully updated or not.
*/
@RequestMapping("/update-user")
@ResponseBody
public String updateUser(long id, String email, String name) {
try {
User user = userDao.findOne(id);
user.setEmail(email);
user.setName(name);
userDao.save(user);
}
catch (Exception ex) {
return "Error while updating the user: " + ex.toString();
}
return "User updated successfully!!";
}
}
pom.xml:
No comments:
Post a Comment