Skip to content

Instantly share code, notes, and snippets.

View SalithaUCSC's full-sized avatar
🚩
Enjoying Life

Salitha Chathuranga SalithaUCSC

🚩
Enjoying Life
View GitHub Profile
@SalithaUCSC
SalithaUCSC / postman-tests.js
Last active May 15, 2021 18:51
postman tests
// https://jsonplaceholder.typicode.com/users
pm.test('Request Name', () => {
pm.expect(pm.info.requestName).equals('Test Postman Scripts');
});
pm.test('Request Method is GET', () => {
pm.expect(pm.request.method).equals('GET');
});
package com.example.demo;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@OpenAPIDefinition(
info = @Info(
package com.example.demo.repositories;
import com.example.demo.entities.Item;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface ItemRepository extends MongoRepository<Item, Long> {
}
package com.example.demo.services;
import com.example.demo.entities.Item;
import java.util.List;
public interface ItemService {
List<Item> getItems();
package com.example.demo.services;
import com.example.demo.entities.Item;
import com.example.demo.repositories.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
package com.example.demo.entities;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "items")
public class Item {
@Id
private String id;
private String name;
private String description;
package com.example.demo.controllers;
import com.example.demo.entities.Item;
import com.example.demo.services.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@SalithaUCSC
SalithaUCSC / ErrorMessage.java
Created January 20, 2021 02:44
ErrorMessage POJO to define the error message throws in case of exceptions
package com.api.reporting.models;
public class ErrorMessage {
private String message;
private String url;
private String port;
private String method;
public ErrorMessage(String message, String url, String port, String method) {
@SalithaUCSC
SalithaUCSC / ProductNotFoundException.java
Created January 20, 2021 02:43
Custom Exception to be used when a Product is not found
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProductNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ProductNotFoundException(String message) {
super(message);
@SalithaUCSC
SalithaUCSC / DefaultExceptionHandler.java
Created January 20, 2021 02:41
Default Exception Handler for REST API
import com.api.reporting.models.ErrorMessage;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice