Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fawad1997 committed Apr 14, 2020
1 parent aa62370 commit b777ed4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ Create package <b>service</b> and create service for every repository. e.g. <b>P
Use **@Service** annotation on PersonService. In PersonService, create private object of PersonRepository and use **@Autowired** annotation on it, so spring framework will initilize that object.

<h4>Creating Controller</h4>

Now in the controller package, create **PersonController** that will manage Http requests.
Use **@RestController**, **@RequestMapping(value = "/person")** as we do in controllers. Create an object of PersonService in PersonController and use **@Autowired** annotation on it, so spring framework will manage object creation.

Now create GET, POST, PUT and DELETE methods with **@GetMapping**,**@PostMapping**, **@PutMapping(value = "/{id}")** and **@DeleteMapping(value = "/{id}")**. In the function parameters, use **@PathVariable int id** to get data from URL and **@RequestBody Person person** to get data from body.

Adding <b>@CrossOrigin(origins = "*", allowedHeaders = "*")</b> on controller so it can be accessed from anywhere.
Adding <b>@CrossOrigin(origins = "*", allowedHeaders = "*")</b> on controller so it can be accessed from anywhere.

Adding **@JsonProperty** on entity to get exact case as we want
6 changes: 6 additions & 0 deletions src/main/java/com/restfulspring/apiexample/entity/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.restfulspring.apiexample.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -16,9 +17,14 @@
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty(value = "ID")
private int ID;
@JsonProperty(value = "Name")
private String Name;
@JsonProperty(value = "Age")
private int Age;
@JsonProperty(value = "Height")
private double Height;
@JsonProperty(value = "CNIC")
private String CNIC;
}

0 comments on commit b777ed4

Please sign in to comment.