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 :: spring data rest relationships 
Java :: thread Exemple 
Java :: single line comment in java 
Java :: python to java convert online 
Java :: Armstrong Numbers Between Two Integers 
Java :: pen default Mail Application in android studio java 
Java :: empty string databinding android 
Java :: java division int by 0 
Java :: if (copy = 5) { in java 
Java :: python to java code conversion 
Java :: How to adjust layout when soft keyboard appears 
Java :: Java remove element at position from array and shift 
Java :: recycler view event being raised multiple times 
Java :: default constructor java 
Java :: java package 
Java :: how to get individual words from a string in java 
Java :: java lambda function 
Java :: check if string is decimal java 
Java :: Java If ... Else 
Java :: finding min and max from given number in java 
Java :: java reverse array 
Java :: Java Access HashMap Elements 
Java :: how to convert integer to list in python 
Java :: mambalam srardham online booking 
Sql :: postgres get size of database 
Sql :: postgresql update sequence next value 
Sql :: find duplicates mysql column 
Sql :: set max_allowed_packet mysql 
Sql :: database url postgres 
Sql :: get all schemas postgres 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =