Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to make a rest api in spring

    @GetMapping(value = "/{id}")
    public Foo findById(@PathVariable("id") Long id) {
        return RestPreconditions.checkFound(service.findById(id));
    }
Comment

spring boot rest api


package com.fixdecode.restcruddemo.customer;
 
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
 
import java.util.Map;
 
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
 
@AllArgsConstructor
@RestController
@RequestMapping("/api/customers/")
public class CustomerController {
    private CustomerService customerService;
 
    // Getting all customers from the database
    @GetMapping
    public ResponseEntity<ResponseMessage> getCustomers(){
        return ResponseEntity.ok(
                ResponseMessage.builder()
                        .data(Map.of("Customers", customerService.getCustomers()))
                        .message("Customers found")
                        .status(OK)
                        .statusCode(OK.value())
                        .build());
    }
    //Adding a new customer to the database
    @PostMapping
    public ResponseEntity<ResponseMessage> addCustomer(@RequestBody Customer customer){
        return ResponseEntity.ok(
                ResponseMessage.builder()
                        .data(Map.of("customer", customerService.saveCustomer(customer)))
                        .message("New customer was added")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build());
    }
 
    //Getting a single customer by the email as id
    @GetMapping("email/{email}")
    public ResponseEntity<ResponseMessage> getCustomer(@PathVariable("email") String email){
        return ResponseEntity.ok(
                ResponseMessage.builder()
                        .data(Map.of("Customer", customerService.getCustomer(email)))
                        .message("Customer found")
                        .status(OK)
                        .statusCode(OK.value())
                        .build());
    }
 
    //Deleting a customer by the id
    @DeleteMapping("delete/{email}")
    public ResponseEntity<ResponseMessage> deleteCustomer(@PathVariable("email") String email){
       return ResponseEntity.ok(
               ResponseMessage.builder()
                       .data(Map.of("Deleted", customerService.deleteCustomer(email)))
                       .message("A customer was deleted")
                       .status(OK)
                       .statusCode(OK.value())
                       .build());
    }
    //Updating customer
    @PutMapping("update")
    public ResponseEntity<ResponseMessage> updateCustomer(@RequestBody Customer customer){
        return ResponseEntity.ok(
                ResponseMessage.builder()
                        .data(Map.of("customer", customerService.updateCustomer(customer)))
                        .message("A customer was updated")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build());
    }
 
 
}
Comment

spring boot rest api

package com.fixdecode.restcruddemo.customer;
 
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import org.hibernate.annotations.NaturalId;
 
import javax.persistence.*;
 
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Customer {
    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @NaturalId
    @NonNull
    private String email;
    private String password;
 
    public Customer(String firstName, String lastName,String email, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Could not resolve project :app 
Java :: fibonacci of 6 
Java :: how to compare doubles in java 
Java :: refreshing method() android studio webview 
Java :: firebase realtime database get key 
Java :: stringbuilder java setlength 
Java :: java packages example 
Java :: insert in arraylist 
Java :: LRU Cache java 
Java :: remove element from char array java 
Java :: what is abstraction in java 
Java :: set style programmatically android 
Java :: hashtable in java 
Java :: java and or precedence 
Java :: random value between 10-20 
Java :: Java List Access Elements using get() method 
Java :: Java Expression statements 
Java :: recyclerview adapter multiple view types 
Java :: firestore java timestamp 
Java :: swagger ui java 
Java :: java draw image 
Java :: can we serialize class in java 
Java :: Add Elements in java map 
Java :: how to create microservices architecture with spring boot 
Java :: deep content 
Java :: l datetime anne month jour heure minute second in java 
Java :: factorial recursion java 
Java :: java modulus opperation 
Java :: url to json 
Java :: Uri.builder in android studio 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =