Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/JavaEETest.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.sang.controller;

import org.sang.entity.Person;
import org.sang.service.PersonService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class PersonController {
private final PersonService personService;

public PersonController(PersonService personService){
this.personService = personService;
}
@RequestMapping("/person")
//Http request for displaying all the person on the list
@GetMapping
public List<Person> getAll(){
return personService.displayAll();
}

//Http request for displaying single person by id
@GetMapping
public Person displayId(Long id){
return personService.displayById(id);
}

//Http request for Creating New Person
@PostMapping
public Person createPerson(@Valid @RequestBody Person person){
return personService.createPerson(person);

}

//Http request for Updating the Person
@PutMapping
public Person updatePerson(@Valid @RequestBody Person person , @PathVariable Long id){
return personService.updatePerson(person, id);
}

//Http Request for Deleting the Person
@DeleteMapping
public void deletePerson(@PathVariable Long id){
personService.deletePerson(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.sang;
package org.sang.entity;

import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Max;

/**
* Created by sang on 2017/1/4.
Expand All @@ -11,8 +14,10 @@
public class Person {
@Id
@GeneratedValue
@NotBlank
private Long id;
private String name;
@Max(18)
private Integer age;
private String address;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sang.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalClassException {
@ExceptionHandler(PersonNotFoundException.class)
ResponseEntity<String> handlePersonNotFoundException(PersonNotFoundException ex){
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(ex.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sang.exception;

public class PersonNotFoundException extends RuntimeException{
public PersonNotFoundException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.sang;
package org.sang.repository;

import org.sang.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Created by sang on 2017/1/4.
*/
@RepositoryRestResource(path = "people")
public interface PersonRepository extends JpaRepository<Person,Long> {
@RestResource(path = "nameStartsWith",rel = "nameStartsWith")
List<Person> findByNameStartsWith(@Param("name") String name);
@Repository
public interface PersonRepository extends JpaRepository<Person, Long>{
}
46 changes: 46 additions & 0 deletions Test23-REST/src/main/java/org/sang/service/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.sang.service;

import org.sang.entity.Person;
import org.sang.repository.PersonRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonService {
private final PersonRepository personRepository;

public PersonService (PersonRepository personRepository){
this.personRepository = personRepository;
}

//Display All the Person
public List<Person> displayAll(){
return personRepository.findAll();
}

//Display Person by ID
public Person displayById(Long id){
return personRepository.findOne(id);
}

//Create New Person
public Person createPerson(Person person){
return personRepository.save(person);
}

//Update the Existing Person
public Person updatePerson(Person updatedPerson, Long id){
Person existingPerson = personRepository.findOne(id);
existingPerson.setName(updatedPerson.getName());
existingPerson.setAddress(updatedPerson.getAddress());
existingPerson.setAge(updatedPerson.getAge());
return personRepository.save(updatedPerson);
}

//Delete the Existing Person
public void deletePerson(Long id){
personRepository.delete(id);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sang;
package org.sang.test;

import java.util.Random;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sang;
package org.sang.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down