BAEL-20884 Move Spring Boot Rest to Spring Boot Modules
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
## Spring Boot REST
|
||||
|
||||
This module contains articles about Spring Boot RESTful APIs.
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [HATEOAS for a Spring REST Service](https://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
- [Versioning a REST API](https://www.baeldung.com/rest-versioning)
|
||||
- [ETags for REST with Spring](https://www.baeldung.com/etags-for-rest-with-spring)
|
||||
- [Testing REST with multiple MIME types](https://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
- [Spring Boot Consuming and Producing JSON](https://www.baeldung.com/spring-boot-json)
|
||||
- [Error Handling for REST with Spring](https://www.baeldung.com/exception-handling-for-rest-with-spring)
|
||||
|
||||
### E-book
|
||||
|
||||
These articles are part of the Spring REST E-book:
|
||||
|
||||
1. [Bootstrap a Web Application with Spring 5](https://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration)
|
||||
2. [Build a REST API with Spring and Java Config](https://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration)
|
||||
3. [Http Message Converters with the Spring Framework](https://www.baeldung.com/spring-httpmessageconverter-rest)
|
||||
4. [Spring’s RequestBody and ResponseBody Annotations](https://www.baeldung.com/spring-request-response-body)
|
||||
5. [Entity To DTO Conversion for a Spring REST API](https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application)
|
||||
6. [Error Handling for REST with Spring](https://www.baeldung.com/exception-handling-for-rest-with-spring)
|
||||
7. [REST API Discoverability and HATEOAS](https://www.baeldung.com/restful-web-service-discoverability)
|
||||
8. [An Intro to Spring HATEOAS](https://www.baeldung.com/spring-hateoas-tutorial)
|
||||
9. [REST Pagination in Spring](https://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
10. [Test a REST API with Java](https://www.baeldung.com/integration-testing-a-rest-api)
|
||||
@@ -1,100 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.web</groupId>
|
||||
<artifactId>spring-boot-rest</artifactId>
|
||||
<name>spring-boot-rest</name>
|
||||
<packaging>war</packaging>
|
||||
<description>Spring Boot Rest Module</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
<!-- We'll need to comment out the jackson-dataformat-xml dependency if we want to use XStream: -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.xstream</groupId>
|
||||
<artifactId>xstream</artifactId>
|
||||
<version>${xstream.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring HATEOAS -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- util -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.htmlunit</groupId>
|
||||
<artifactId>htmlunit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.modelmapper</groupId>
|
||||
<artifactId>modelmapper</artifactId>
|
||||
<version>${modelmapper.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.SpringBootRestApplication</start-class>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
<xstream.version>1.4.11.1</xstream.version>
|
||||
<modelmapper.version>2.3.5</modelmapper.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootRestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootRestApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ModelMapper modelMapper() {
|
||||
return new ModelMapper();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.persistence;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public interface IOperations<T extends Serializable> {
|
||||
|
||||
// read - one
|
||||
|
||||
T findById(final long id);
|
||||
|
||||
// read - all
|
||||
|
||||
List<T> findAll();
|
||||
|
||||
Page<T> findPaginated(int page, int size);
|
||||
|
||||
// write
|
||||
|
||||
T create(final T entity);
|
||||
|
||||
T update(final T entity);
|
||||
|
||||
void delete(final T entity);
|
||||
|
||||
void deleteById(final long entityId);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooDao extends JpaRepository<Foo, Long> {
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
public class Customer extends ResourceSupport {
|
||||
private String customerId;
|
||||
private String customerName;
|
||||
private String companyName;
|
||||
private Map<String, Order> orders;
|
||||
|
||||
public Customer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Customer(final String customerId, final String customerName, final String companyName) {
|
||||
super();
|
||||
this.customerId = customerId;
|
||||
this.customerName = customerName;
|
||||
this.companyName = companyName;
|
||||
}
|
||||
|
||||
public String getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(final String customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(final String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public String getCompanyName() {
|
||||
return companyName;
|
||||
}
|
||||
|
||||
public void setCompanyName(final String companyName) {
|
||||
this.companyName = companyName;
|
||||
}
|
||||
|
||||
public Map<String, Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public void setOrders(final Map<String, Order> orders) {
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("Foo")
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Foo(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Foo other = (Foo) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Foo [name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
|
||||
public class Order extends ResourceSupport {
|
||||
private String orderId;
|
||||
private double price;
|
||||
private int quantity;
|
||||
|
||||
public Order() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Order(final String orderId, final double price, final int quantity) {
|
||||
super();
|
||||
this.orderId = orderId;
|
||||
this.price = price;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public String getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(final String orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(final double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(final int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order [orderId=" + orderId + ", price=" + price + ", quantity=" + quantity + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.persistence.service;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import com.baeldung.persistence.IOperations;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
||||
public interface IFooService extends IOperations<Foo> {
|
||||
|
||||
Page<Foo> findPaginated(Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.persistence.service.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.persistence.IOperations;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@Transactional
|
||||
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
|
||||
|
||||
// read - one
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public T findById(final long id) {
|
||||
return getDao().findById(id).orElse(null);
|
||||
}
|
||||
|
||||
// read - all
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> findAll() {
|
||||
return Lists.newArrayList(getDao().findAll());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findPaginated(final int page, final int size) {
|
||||
return getDao().findAll(PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
// write
|
||||
|
||||
@Override
|
||||
public T create(final T entity) {
|
||||
return getDao().save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T update(final T entity) {
|
||||
return getDao().save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final T entity) {
|
||||
getDao().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(final long entityId) {
|
||||
getDao().deleteById(entityId);
|
||||
}
|
||||
|
||||
protected abstract PagingAndSortingRepository<T, Long> getDao();
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.baeldung.persistence.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.persistence.dao.IFooDao;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.service.IFooService;
|
||||
import com.baeldung.persistence.service.common.AbstractService;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class FooService extends AbstractService<Foo> implements IFooService {
|
||||
|
||||
@Autowired
|
||||
private IFooDao dao;
|
||||
|
||||
public FooService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
protected PagingAndSortingRepository<Foo, Long> getDao() {
|
||||
return dao;
|
||||
}
|
||||
|
||||
// custom methods
|
||||
|
||||
@Override
|
||||
public Page<Foo> findPaginated(Pageable pageable) {
|
||||
return dao.findAll(pageable);
|
||||
}
|
||||
|
||||
// overridden to be secured
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Foo> findAll() {
|
||||
return Lists.newArrayList(getDao().findAll());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.requestresponsebody;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/post")
|
||||
public class ExamplePostController {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ExamplePostController.class);
|
||||
|
||||
@Autowired ExampleService exampleService;
|
||||
|
||||
@PostMapping("/request")
|
||||
public ResponseEntity postController(@RequestBody LoginForm loginForm) {
|
||||
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
|
||||
exampleService.fakeAuthenticate(loginForm);
|
||||
return ResponseEntity.ok(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/response")
|
||||
@ResponseBody
|
||||
public ResponseTransfer postResponseController(@RequestBody LoginForm loginForm) {
|
||||
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
|
||||
return new ResponseTransfer("Thanks For Posting!!!");
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.requestresponsebody;
|
||||
|
||||
public class LoginForm {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public LoginForm() {
|
||||
}
|
||||
|
||||
public LoginForm(String username, String password) {
|
||||
super();
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.requestresponsebody;
|
||||
|
||||
public class ResponseTransfer {
|
||||
|
||||
private String text;
|
||||
|
||||
public ResponseTransfer(String text) {
|
||||
this.setText(text);
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
|
||||
public interface CustomerService {
|
||||
|
||||
List<Customer> allCustomers();
|
||||
|
||||
Customer getCustomerDetail(final String id);
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
|
||||
@Service
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
||||
private HashMap<String, Customer> customerMap;
|
||||
|
||||
public CustomerServiceImpl() {
|
||||
|
||||
customerMap = new HashMap<>();
|
||||
|
||||
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
|
||||
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
|
||||
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
|
||||
|
||||
customerMap.put("10A", customerOne);
|
||||
customerMap.put("20B", customerTwo);
|
||||
customerMap.put("30C", customerThree);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> allCustomers() {
|
||||
return new ArrayList<>(customerMap.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCustomerDetail(final String customerId) {
|
||||
return customerMap.get(customerId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.requestresponsebody.LoginForm;
|
||||
|
||||
@Service
|
||||
public class ExampleService {
|
||||
|
||||
public boolean fakeAuthenticate(LoginForm lf) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.persistence.model.Order;
|
||||
|
||||
public interface OrderService {
|
||||
|
||||
List<Order> getAllOrdersForCustomer(String customerId);
|
||||
|
||||
Order getOrderByIdForCustomer(String customerId, String orderId);
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
private HashMap<String, Customer> customerMap;
|
||||
private HashMap<String, Order> customerOneOrderMap;
|
||||
private HashMap<String, Order> customerTwoOrderMap;
|
||||
private HashMap<String, Order> customerThreeOrderMap;
|
||||
|
||||
public OrderServiceImpl() {
|
||||
|
||||
customerMap = new HashMap<>();
|
||||
customerOneOrderMap = new HashMap<>();
|
||||
customerTwoOrderMap = new HashMap<>();
|
||||
customerThreeOrderMap = new HashMap<>();
|
||||
|
||||
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
|
||||
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
|
||||
|
||||
customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325));
|
||||
customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525));
|
||||
|
||||
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
|
||||
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
|
||||
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
|
||||
|
||||
customerOne.setOrders(customerOneOrderMap);
|
||||
customerTwo.setOrders(customerTwoOrderMap);
|
||||
customerThree.setOrders(customerThreeOrderMap);
|
||||
customerMap.put("10A", customerOne);
|
||||
customerMap.put("20B", customerTwo);
|
||||
customerMap.put("30C", customerThree);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getAllOrdersForCustomer(final String customerId) {
|
||||
return new ArrayList<>(customerMap.get(customerId).getOrders().values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
|
||||
final Map<String, Order> orders = customerMap.get(customerId).getOrders();
|
||||
Order selectedOrder = orders.get(orderId);
|
||||
return selectedOrder;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
|
||||
@ComponentScan(basePackages = { "com.baeldung.persistence", "com.baeldung.springpagination" })
|
||||
@EnableJpaRepositories(basePackages = {"com.baeldung.persistence.dao", "com.baeldung.springpagination.repository"})
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "com.baeldung.persistence.model", "com.baeldung.springpagination.model" });
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
// vendorAdapter.set
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
import org.springframework.web.filter.ShallowEtagHeaderFilter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
// If we want to enable xml configurations for message-converter:
|
||||
// @ImportResource("classpath:WEB-INF/api-servlet.xml")
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
// @Override
|
||||
// public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
|
||||
// messageConverters.add(new MappingJackson2HttpMessageConverter());
|
||||
// messageConverters.add(createXmlHttpMessageConverter());
|
||||
// }
|
||||
//
|
||||
// private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
|
||||
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
|
||||
//
|
||||
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
|
||||
// xstreamMarshaller.setAutodetectAnnotations(true);
|
||||
// xmlConverter.setMarshaller(xstreamMarshaller);
|
||||
// xmlConverter.setUnmarshaller(xstreamMarshaller);
|
||||
//
|
||||
// return xmlConverter;
|
||||
// }
|
||||
|
||||
// Another possibility is to create a bean which will be automatically added to the Spring Boot Autoconfigurations
|
||||
// @Bean
|
||||
// public HttpMessageConverter<Object> createXmlHttpMessageConverter() {
|
||||
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
|
||||
//
|
||||
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
|
||||
// xstreamMarshaller.setAutodetectAnnotations(true);
|
||||
// xmlConverter.setMarshaller(xstreamMarshaller);
|
||||
// xmlConverter.setUnmarshaller(xstreamMarshaller);
|
||||
//
|
||||
// return xmlConverter;
|
||||
// }
|
||||
|
||||
// Etags
|
||||
|
||||
// If we're not using Spring Boot we can make use of
|
||||
// AbstractAnnotationConfigDispatcherServletInitializer#getServletFilters
|
||||
@Bean
|
||||
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
|
||||
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter());
|
||||
filterRegistrationBean.addUrlPatterns("/foos/*");
|
||||
filterRegistrationBean.setName("etagFilter");
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
|
||||
// We can also just declare the filter directly
|
||||
// @Bean
|
||||
// public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
|
||||
// return new ShallowEtagHeaderFilter();
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package com.baeldung.springpagination.controller;
|
||||
|
||||
import com.baeldung.springpagination.dto.PostDto;
|
||||
import com.baeldung.springpagination.model.Post;
|
||||
import com.baeldung.springpagination.service.IPostService;
|
||||
import com.baeldung.springpagination.service.IUserService;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/posts")
|
||||
public class PostRestController {
|
||||
|
||||
@Autowired
|
||||
private IPostService postService;
|
||||
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@Autowired
|
||||
private ModelMapper modelMapper;
|
||||
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public List<PostDto> getPosts(
|
||||
@PathVariable("page") int page,
|
||||
@PathVariable("size") int size,
|
||||
@PathVariable("sortDir") String sortDir,
|
||||
@PathVariable("sort") String sort) {
|
||||
|
||||
List<Post> posts = postService.getPostsList(page, size, sortDir, sort);
|
||||
return posts.stream()
|
||||
.map(this::convertToDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@ResponseBody
|
||||
public PostDto createPost(@RequestBody PostDto postDto) throws ParseException {
|
||||
Post post = convertToEntity(postDto);
|
||||
Post postCreated = postService.createPost(post);
|
||||
return convertToDto(postCreated);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
@ResponseBody
|
||||
public PostDto getPost(@PathVariable("id") Long id) {
|
||||
return convertToDto(postService.getPostById(id));
|
||||
}
|
||||
|
||||
@PutMapping(value = "/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void updatePost(@RequestBody PostDto postDto) throws ParseException {
|
||||
Post post = convertToEntity(postDto);
|
||||
postService.updatePost(post);
|
||||
}
|
||||
|
||||
|
||||
private PostDto convertToDto(Post post) {
|
||||
PostDto postDto = modelMapper.map(post, PostDto.class);
|
||||
postDto.setSubmissionDate(post.getSubmissionDate(),
|
||||
userService.getCurrentUser().getPreference().getTimezone());
|
||||
return postDto;
|
||||
}
|
||||
|
||||
private Post convertToEntity(PostDto postDto) throws ParseException {
|
||||
Post post = modelMapper.map(postDto, Post.class);
|
||||
post.setSubmissionDate(postDto.getSubmissionDateConverted(
|
||||
userService.getCurrentUser().getPreference().getTimezone()));
|
||||
|
||||
if (postDto.getId() != null) {
|
||||
Post oldPost = postService.getPostById(postDto.getId());
|
||||
post.setRedditID(oldPost.getRedditID());
|
||||
post.setSent(oldPost.isSent());
|
||||
}
|
||||
return post;
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.baeldung.springpagination.dto;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class PostDto {
|
||||
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String url;
|
||||
|
||||
private String date;
|
||||
|
||||
private UserDto user;
|
||||
|
||||
public Date getSubmissionDateConverted(String timezone) throws ParseException {
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
|
||||
return dateFormat.parse(this.date);
|
||||
}
|
||||
|
||||
public void setSubmissionDate(Date date, String timezone) {
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
|
||||
this.date = dateFormat.format(date);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public UserDto getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(UserDto user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.springpagination.dto;
|
||||
|
||||
public class UserDto {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.baeldung.springpagination.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Post {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String url;
|
||||
|
||||
private String date;
|
||||
|
||||
private String redditID;
|
||||
|
||||
private Date submissionDate;
|
||||
|
||||
private boolean sent;
|
||||
|
||||
private String userName;
|
||||
|
||||
public Post() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isSent() {
|
||||
return sent;
|
||||
}
|
||||
|
||||
public void setSent(boolean sent) {
|
||||
this.sent = sent;
|
||||
}
|
||||
|
||||
public String getRedditID() {
|
||||
return redditID;
|
||||
}
|
||||
|
||||
public void setRedditID(String redditID) {
|
||||
this.redditID = redditID;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Date getSubmissionDate() {
|
||||
return submissionDate;
|
||||
}
|
||||
|
||||
public void setSubmissionDate(Date submissionDate) {
|
||||
this.submissionDate = submissionDate;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.springpagination.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Preference {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String timezone;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTimezone() {
|
||||
return timezone;
|
||||
}
|
||||
|
||||
public void setTimezone(String timezone) {
|
||||
this.timezone = timezone;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.springpagination.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Subject {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
public Subject() {
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.springpagination.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne
|
||||
Preference preference;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Preference getPreference() {
|
||||
return preference;
|
||||
}
|
||||
|
||||
public void setPreference(Preference preference) {
|
||||
this.preference = preference;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.springpagination.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.springpagination.model.Post;
|
||||
import com.baeldung.springpagination.model.User;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, Long>, PagingAndSortingRepository<Post, Long> {
|
||||
|
||||
@Query("select u from Post u where u.userName=:userName")
|
||||
Page<Post> findByUser(@Param("userName") String userName, Pageable pageReq);
|
||||
|
||||
default Page<Post> findByUser(User user, Pageable pageReq) {
|
||||
return findByUser(user.getName(), pageReq);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.springpagination.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
|
||||
import com.baeldung.springpagination.model.Subject;
|
||||
|
||||
public interface SubjectRepository extends PagingAndSortingRepository<Subject, Long> {
|
||||
|
||||
@RestResource(path = "nameContains")
|
||||
public Page<Subject> findByNameContaining(@Param("name") String name, Pageable p);
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.springpagination.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.springpagination.model.Post;
|
||||
|
||||
public interface IPostService {
|
||||
|
||||
List<Post> getPostsList(int page, int size, String sortDir, String sort);
|
||||
|
||||
void updatePost(Post post);
|
||||
|
||||
Post createPost(Post post);
|
||||
|
||||
Post getPostById(Long id);
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.springpagination.service;
|
||||
|
||||
import com.baeldung.springpagination.model.User;
|
||||
|
||||
public interface IUserService {
|
||||
|
||||
User getCurrentUser();
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.springpagination.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.springpagination.model.Post;
|
||||
import com.baeldung.springpagination.repository.PostRepository;
|
||||
|
||||
@Service
|
||||
public class PostService implements IPostService {
|
||||
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@Override
|
||||
public List<Post> getPostsList(int page, int size, String sortDir, String sort) {
|
||||
|
||||
PageRequest pageReq
|
||||
= PageRequest.of(page, size, Sort.Direction.fromString(sortDir), sort);
|
||||
|
||||
Page<Post> posts = postRepository.findByUser(userService.getCurrentUser(), pageReq);
|
||||
return posts.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePost(Post post) {
|
||||
postRepository.save(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post createPost(Post post) {
|
||||
return postRepository.save(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long id) {
|
||||
return postRepository.getOne(id);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.springpagination.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.springpagination.model.Preference;
|
||||
import com.baeldung.springpagination.model.User;
|
||||
|
||||
@Service
|
||||
public class UserService implements IUserService {
|
||||
|
||||
@Override
|
||||
public User getCurrentUser() {
|
||||
|
||||
Preference preference = new Preference();
|
||||
preference.setId(1L);
|
||||
preference.setTimezone("Asia/Calcutta");
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("Micheal");
|
||||
user.setPreference(preference);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.web.config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
@Component
|
||||
public class MyCustomErrorAttributes extends DefaultErrorAttributes {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
|
||||
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
|
||||
errorAttributes.put("locale", webRequest.getLocale()
|
||||
.toString());
|
||||
errorAttributes.remove("error");
|
||||
errorAttributes.put("cause", errorAttributes.get("message"));
|
||||
errorAttributes.remove("message");
|
||||
errorAttributes.put("status", String.valueOf(errorAttributes.get("status")));
|
||||
return errorAttributes;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.web.config;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ErrorProperties;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
|
||||
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Component
|
||||
public class MyErrorController extends BasicErrorController {
|
||||
|
||||
public MyErrorController(ErrorAttributes errorAttributes) {
|
||||
super(errorAttributes, new ErrorProperties());
|
||||
}
|
||||
|
||||
@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
|
||||
public ResponseEntity<Map<String, Object>> xmlError(HttpServletRequest request) {
|
||||
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.APPLICATION_XML));
|
||||
body.put("xmlkey", "the XML response is different!");
|
||||
HttpStatus status = getStatus(request);
|
||||
return new ResponseEntity<>(body, status);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Resources;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
import com.baeldung.services.CustomerService;
|
||||
import com.baeldung.services.OrderService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/customers")
|
||||
@EnableHypermediaSupport(type = HypermediaType.HAL)
|
||||
public class CustomerController {
|
||||
@Autowired
|
||||
private CustomerService customerService;
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@GetMapping("/{customerId}")
|
||||
public Customer getCustomerById(@PathVariable final String customerId) {
|
||||
return customerService.getCustomerDetail(customerId);
|
||||
}
|
||||
|
||||
@GetMapping("/{customerId}/{orderId}")
|
||||
public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) {
|
||||
return orderService.getOrderByIdForCustomer(customerId, orderId);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{customerId}/orders", produces = { "application/hal+json" })
|
||||
public Resources<Order> getOrdersForCustomer(@PathVariable final String customerId) {
|
||||
final List<Order> orders = orderService.getAllOrdersForCustomer(customerId);
|
||||
for (final Order order : orders) {
|
||||
final Link selfLink = linkTo(
|
||||
methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
|
||||
order.add(selfLink);
|
||||
}
|
||||
|
||||
Link link = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel();
|
||||
Resources<Order> result = new Resources<>(orders, link);
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping(produces = { "application/hal+json" })
|
||||
public Resources<Customer> getAllCustomers() {
|
||||
final List<Customer> allCustomers = customerService.allCustomers();
|
||||
|
||||
for (final Customer customer : allCustomers) {
|
||||
String customerId = customer.getCustomerId();
|
||||
Link selfLink = linkTo(CustomerController.class).slash(customerId)
|
||||
.withSelfRel();
|
||||
customer.add(selfLink);
|
||||
if (orderService.getAllOrdersForCustomer(customerId)
|
||||
.size() > 0) {
|
||||
final Link ordersLink = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId))
|
||||
.withRel("allOrders");
|
||||
customer.add(ordersLink);
|
||||
}
|
||||
}
|
||||
|
||||
Link link = linkTo(CustomerController.class).withSelfRel();
|
||||
Resources<Customer> result = new Resources<>(allCustomers, link);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class FaultyRestController {
|
||||
|
||||
@GetMapping("/exception")
|
||||
public ResponseEntity<Void> requestWithException() {
|
||||
throw new RuntimeException("Error in the faulty controller!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.service.IFooService;
|
||||
import com.baeldung.web.exception.MyResourceNotFoundException;
|
||||
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
|
||||
import com.baeldung.web.hateoas.event.ResourceCreatedEvent;
|
||||
import com.baeldung.web.hateoas.event.SingleResourceRetrievedEvent;
|
||||
import com.baeldung.web.util.RestPreconditions;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/foos")
|
||||
public class FooController {
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
@Autowired
|
||||
private IFooService service;
|
||||
|
||||
public FooController() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
// Note: the global filter overrides the ETag value we set here. We can still analyze its behaviour in the Integration Test.
|
||||
@GetMapping(value = "/{id}/custom-etag")
|
||||
public ResponseEntity<Foo> findByIdWithCustomEtag(@PathVariable("id") final Long id,
|
||||
final HttpServletResponse response) {
|
||||
final Foo foo = RestPreconditions.checkFound(service.findById(id));
|
||||
|
||||
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
||||
return ResponseEntity.ok()
|
||||
.eTag(Long.toString(foo.getVersion()))
|
||||
.body(foo);
|
||||
}
|
||||
|
||||
// read - one
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
|
||||
try {
|
||||
final Foo resourceById = RestPreconditions.checkFound(service.findById(id));
|
||||
|
||||
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
||||
return resourceById;
|
||||
}
|
||||
catch (MyResourceNotFoundException exc) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.NOT_FOUND, "Foo Not Found", exc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// read - all
|
||||
|
||||
@GetMapping
|
||||
public List<Foo> findAll() {
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
@GetMapping(params = { "page", "size" })
|
||||
public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size,
|
||||
final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
||||
final Page<Foo> resultPage = service.findPaginated(page, size);
|
||||
if (page > resultPage.getTotalPages()) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response, page,
|
||||
resultPage.getTotalPages(), size));
|
||||
|
||||
return resultPage.getContent();
|
||||
}
|
||||
|
||||
@GetMapping("/pageable")
|
||||
public List<Foo> findPaginatedWithPageable(Pageable pageable, final UriComponentsBuilder uriBuilder,
|
||||
final HttpServletResponse response) {
|
||||
final Page<Foo> resultPage = service.findPaginated(pageable);
|
||||
if (pageable.getPageNumber() > resultPage.getTotalPages()) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response,
|
||||
pageable.getPageNumber(), resultPage.getTotalPages(), pageable.getPageSize()));
|
||||
|
||||
return resultPage.getContent();
|
||||
}
|
||||
|
||||
// write
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
final Foo foo = service.create(resource);
|
||||
final Long idOfCreatedResource = foo.getId();
|
||||
|
||||
eventPublisher.publishEvent(new ResourceCreatedEvent(this, response, idOfCreatedResource));
|
||||
|
||||
return foo;
|
||||
}
|
||||
|
||||
@PutMapping(value = "/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
RestPreconditions.checkFound(service.findById(resource.getId()));
|
||||
service.update(resource);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void delete(@PathVariable("id") final Long id) {
|
||||
service.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
|
||||
import com.baeldung.web.util.LinkUtil;
|
||||
|
||||
@Controller
|
||||
public class RootController {
|
||||
|
||||
// API
|
||||
|
||||
// discover
|
||||
|
||||
@GetMapping("/")
|
||||
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
||||
public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) {
|
||||
final String rootUri = request.getRequestURL()
|
||||
.toString();
|
||||
|
||||
final URI fooUri = new UriTemplate("{rootUri}{resource}").expand(rootUri, "foos");
|
||||
final String linkToFoos = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection");
|
||||
response.addHeader("Link", linkToFoos);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.baeldung.web.controller.students;
|
||||
|
||||
public class Student {
|
||||
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Student() {}
|
||||
|
||||
public Student(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Student(long id, String firstName, String lastName) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package com.baeldung.web.controller.students;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import com.baeldung.web.controller.students.StudentService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/students")
|
||||
public class StudentController {
|
||||
|
||||
@Autowired
|
||||
private StudentService service;
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Student> read() {
|
||||
return service.readAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Student> read(@PathVariable("id") Long id) {
|
||||
Student foundStudent = service.read(id);
|
||||
if (foundStudent == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
} else {
|
||||
return ResponseEntity.ok(foundStudent);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public ResponseEntity<Student> create(@RequestBody Student student) throws URISyntaxException {
|
||||
Student createdStudent = service.create(student);
|
||||
|
||||
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
|
||||
.path("/{id}")
|
||||
.buildAndExpand(createdStudent.getId())
|
||||
.toUri();
|
||||
|
||||
return ResponseEntity.created(uri)
|
||||
.body(createdStudent);
|
||||
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<Student> update(@RequestBody Student student, @PathVariable Long id) {
|
||||
Student updatedStudent = service.update(id, student);
|
||||
if (updatedStudent == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
} else {
|
||||
return ResponseEntity.ok(updatedStudent);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Object> deleteStudent(@PathVariable Long id) {
|
||||
service.delete(id);
|
||||
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.baeldung.web.controller.students;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StudentService {
|
||||
|
||||
// DB repository mock
|
||||
private Map<Long, Student> repository = Arrays.asList(
|
||||
new Student[]{
|
||||
new Student(1, "Alan","Turing"),
|
||||
new Student(2, "Sebastian","Bach"),
|
||||
new Student(3, "Pablo","Picasso"),
|
||||
}).stream()
|
||||
.collect(Collectors.toConcurrentMap(s -> s.getId(), Function.identity()));
|
||||
|
||||
// DB id sequence mock
|
||||
private AtomicLong sequence = new AtomicLong(3);
|
||||
|
||||
public List<Student> readAll() {
|
||||
return repository.values().stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Student read(Long id) {
|
||||
return repository.get(id);
|
||||
}
|
||||
|
||||
public Student create(Student student) {
|
||||
long key = sequence.incrementAndGet();
|
||||
student.setId(key);
|
||||
repository.put(key, student);
|
||||
return student;
|
||||
}
|
||||
|
||||
public Student update(Long id, Student student) {
|
||||
student.setId(id);
|
||||
Student oldStudent = repository.replace(id, student);
|
||||
return oldStudent == null ? null : student;
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
repository.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.baeldung.web.error;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import com.baeldung.web.exception.MyResourceNotFoundException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
public RestResponseEntityExceptionHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
// 400
|
||||
|
||||
@ExceptionHandler({ ConstraintViolationException.class })
|
||||
public ResponseEntity<Object> handleBadRequest(final ConstraintViolationException ex, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ DataIntegrityViolationException.class })
|
||||
public ResponseEntity<Object> handleBadRequest(final DataIntegrityViolationException ex, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
// ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on
|
||||
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
|
||||
|
||||
// 404
|
||||
|
||||
@ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
|
||||
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
||||
}
|
||||
|
||||
// 409
|
||||
|
||||
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class })
|
||||
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
|
||||
}
|
||||
|
||||
// 412
|
||||
|
||||
// 500
|
||||
|
||||
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
|
||||
/*500*/public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
|
||||
logger.error("500 Status Code", ex);
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
||||
public class BadRequestException extends RuntimeException {
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
public final class MyResourceNotFoundException extends RuntimeException {
|
||||
|
||||
public MyResourceNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyResourceNotFoundException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public MyResourceNotFoundException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MyResourceNotFoundException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(value = HttpStatus.NOT_FOUND)
|
||||
public class ResourceNotFoundException extends RuntimeException {
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.baeldung.web.hateoas.event;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Event that is fired when a paginated search is performed.
|
||||
* <p/>
|
||||
* This event object contains all the information needed to create the URL for the paginated results
|
||||
*
|
||||
* @param <T>
|
||||
* Type of the result that is being handled (commonly Entities).
|
||||
*/
|
||||
public final class PaginatedResultsRetrievedEvent<T extends Serializable> extends ApplicationEvent {
|
||||
private final UriComponentsBuilder uriBuilder;
|
||||
private final HttpServletResponse response;
|
||||
private final int page;
|
||||
private final int totalPages;
|
||||
private final int pageSize;
|
||||
|
||||
public PaginatedResultsRetrievedEvent(final Class<T> clazz, final UriComponentsBuilder uriBuilderToSet, final HttpServletResponse responseToSet, final int pageToSet, final int totalPagesToSet, final int pageSizeToSet) {
|
||||
super(clazz);
|
||||
|
||||
uriBuilder = uriBuilderToSet;
|
||||
response = responseToSet;
|
||||
page = pageToSet;
|
||||
totalPages = totalPagesToSet;
|
||||
pageSize = pageSizeToSet;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public final UriComponentsBuilder getUriBuilder() {
|
||||
return uriBuilder;
|
||||
}
|
||||
|
||||
public final HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public final int getPage() {
|
||||
return page;
|
||||
}
|
||||
|
||||
public final int getTotalPages() {
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
public final int getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* The object on which the Event initially occurred.
|
||||
*
|
||||
* @return The object on which the Event initially occurred.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public final Class<T> getClazz() {
|
||||
return (Class<T>) getSource();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.web.hateoas.event;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class ResourceCreatedEvent extends ApplicationEvent {
|
||||
private final HttpServletResponse response;
|
||||
private final long idOfNewResource;
|
||||
|
||||
public ResourceCreatedEvent(final Object source, final HttpServletResponse response, final long idOfNewResource) {
|
||||
super(source);
|
||||
|
||||
this.response = response;
|
||||
this.idOfNewResource = idOfNewResource;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public long getIdOfNewResource() {
|
||||
return idOfNewResource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.web.hateoas.event;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class SingleResourceRetrievedEvent extends ApplicationEvent {
|
||||
private final HttpServletResponse response;
|
||||
|
||||
public SingleResourceRetrievedEvent(final Object source, final HttpServletResponse response) {
|
||||
super(source);
|
||||
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
package com.baeldung.web.hateoas.listener;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
|
||||
import com.baeldung.web.util.LinkUtil;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
@Component
|
||||
class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationListener<PaginatedResultsRetrievedEvent> {
|
||||
|
||||
private static final String PAGE = "page";
|
||||
|
||||
public PaginatedResultsRetrievedDiscoverabilityListener() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final void onApplicationEvent(final PaginatedResultsRetrievedEvent ev) {
|
||||
Preconditions.checkNotNull(ev);
|
||||
|
||||
addLinkHeaderOnPagedResourceRetrieval(ev.getUriBuilder(), ev.getResponse(), ev.getClazz(), ev.getPage(),
|
||||
ev.getTotalPages(), ev.getPageSize());
|
||||
}
|
||||
|
||||
// - note: at this point, the URI is transformed into plural (added `s`) in a hardcoded way - this will change in the future
|
||||
final void addLinkHeaderOnPagedResourceRetrieval(final UriComponentsBuilder uriBuilder,
|
||||
final HttpServletResponse response, final Class clazz, final int page, final int totalPages,
|
||||
final int pageSize) {
|
||||
plural(uriBuilder, clazz);
|
||||
|
||||
final StringJoiner linkHeader = new StringJoiner(", ");
|
||||
if (hasNextPage(page, totalPages)) {
|
||||
final String uriForNextPage = constructNextPageUri(uriBuilder, page, pageSize);
|
||||
linkHeader.add(LinkUtil.createLinkHeader(uriForNextPage, LinkUtil.REL_NEXT));
|
||||
}
|
||||
if (hasPreviousPage(page)) {
|
||||
final String uriForPrevPage = constructPrevPageUri(uriBuilder, page, pageSize);
|
||||
linkHeader.add(LinkUtil.createLinkHeader(uriForPrevPage, LinkUtil.REL_PREV));
|
||||
}
|
||||
if (hasFirstPage(page)) {
|
||||
final String uriForFirstPage = constructFirstPageUri(uriBuilder, pageSize);
|
||||
linkHeader.add(LinkUtil.createLinkHeader(uriForFirstPage, LinkUtil.REL_FIRST));
|
||||
}
|
||||
if (hasLastPage(page, totalPages)) {
|
||||
final String uriForLastPage = constructLastPageUri(uriBuilder, totalPages, pageSize);
|
||||
linkHeader.add(LinkUtil.createLinkHeader(uriForLastPage, LinkUtil.REL_LAST));
|
||||
}
|
||||
|
||||
if (linkHeader.length() > 0) {
|
||||
response.addHeader(HttpHeaders.LINK, linkHeader.toString());
|
||||
}
|
||||
}
|
||||
|
||||
final String constructNextPageUri(final UriComponentsBuilder uriBuilder, final int page, final int size) {
|
||||
return uriBuilder.replaceQueryParam(PAGE, page + 1)
|
||||
.replaceQueryParam("size", size)
|
||||
.build()
|
||||
.encode()
|
||||
.toUriString();
|
||||
}
|
||||
|
||||
final String constructPrevPageUri(final UriComponentsBuilder uriBuilder, final int page, final int size) {
|
||||
return uriBuilder.replaceQueryParam(PAGE, page - 1)
|
||||
.replaceQueryParam("size", size)
|
||||
.build()
|
||||
.encode()
|
||||
.toUriString();
|
||||
}
|
||||
|
||||
final String constructFirstPageUri(final UriComponentsBuilder uriBuilder, final int size) {
|
||||
return uriBuilder.replaceQueryParam(PAGE, 0)
|
||||
.replaceQueryParam("size", size)
|
||||
.build()
|
||||
.encode()
|
||||
.toUriString();
|
||||
}
|
||||
|
||||
final String constructLastPageUri(final UriComponentsBuilder uriBuilder, final int totalPages, final int size) {
|
||||
return uriBuilder.replaceQueryParam(PAGE, totalPages)
|
||||
.replaceQueryParam("size", size)
|
||||
.build()
|
||||
.encode()
|
||||
.toUriString();
|
||||
}
|
||||
|
||||
final boolean hasNextPage(final int page, final int totalPages) {
|
||||
return page < (totalPages - 1);
|
||||
}
|
||||
|
||||
final boolean hasPreviousPage(final int page) {
|
||||
return page > 0;
|
||||
}
|
||||
|
||||
final boolean hasFirstPage(final int page) {
|
||||
return hasPreviousPage(page);
|
||||
}
|
||||
|
||||
final boolean hasLastPage(final int page, final int totalPages) {
|
||||
return (totalPages > 1) && hasNextPage(page, totalPages);
|
||||
}
|
||||
|
||||
// template
|
||||
|
||||
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
|
||||
final String resourceName = clazz.getSimpleName()
|
||||
.toLowerCase() + "s";
|
||||
uriBuilder.path("/" + resourceName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.web.hateoas.listener;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.http.HttpHeaders;
|
||||
import com.baeldung.web.hateoas.event.ResourceCreatedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Component
|
||||
class ResourceCreatedDiscoverabilityListener implements ApplicationListener<ResourceCreatedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final ResourceCreatedEvent resourceCreatedEvent) {
|
||||
Preconditions.checkNotNull(resourceCreatedEvent);
|
||||
|
||||
final HttpServletResponse response = resourceCreatedEvent.getResponse();
|
||||
final long idOfNewResource = resourceCreatedEvent.getIdOfNewResource();
|
||||
|
||||
addLinkHeaderOnResourceCreation(response, idOfNewResource);
|
||||
}
|
||||
|
||||
void addLinkHeaderOnResourceCreation(final HttpServletResponse response, final long idOfNewResource) {
|
||||
// final String requestUrl = request.getRequestURL().toString();
|
||||
// final URI uri = new UriTemplate("{requestUrl}/{idOfNewResource}").expand(requestUrl, idOfNewResource);
|
||||
|
||||
final URI uri = ServletUriComponentsBuilder.fromCurrentRequestUri().path("/{idOfNewResource}").buildAndExpand(idOfNewResource).toUri();
|
||||
response.setHeader(HttpHeaders.LOCATION, uri.toASCIIString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.baeldung.web.hateoas.listener;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.baeldung.web.hateoas.event.SingleResourceRetrievedEvent;
|
||||
import com.baeldung.web.util.LinkUtil;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
@Component
|
||||
class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListener<SingleResourceRetrievedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) {
|
||||
Preconditions.checkNotNull(resourceRetrievedEvent);
|
||||
|
||||
final HttpServletResponse response = resourceRetrievedEvent.getResponse();
|
||||
addLinkHeaderOnSingleResourceRetrieval(response);
|
||||
}
|
||||
|
||||
void addLinkHeaderOnSingleResourceRetrieval(final HttpServletResponse response) {
|
||||
final String requestURL = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri().toASCIIString();
|
||||
final int positionOfLastSlash = requestURL.lastIndexOf("/");
|
||||
final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);
|
||||
|
||||
final String linkHeaderValue = LinkUtil.createLinkHeader(uriForResourceCreation, "collection");
|
||||
response.addHeader(HttpHeaders.LINK, linkHeaderValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.web.util;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* Provides some constants and utility methods to build a Link Header to be stored in the {@link HttpServletResponse} object
|
||||
*/
|
||||
public final class LinkUtil {
|
||||
|
||||
public static final String REL_COLLECTION = "collection";
|
||||
public static final String REL_NEXT = "next";
|
||||
public static final String REL_PREV = "prev";
|
||||
public static final String REL_FIRST = "first";
|
||||
public static final String REL_LAST = "last";
|
||||
|
||||
private LinkUtil() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
/**
|
||||
* Creates a Link Header to be stored in the {@link HttpServletResponse} to provide Discoverability features to the user
|
||||
*
|
||||
* @param uri
|
||||
* the base uri
|
||||
* @param rel
|
||||
* the relative path
|
||||
*
|
||||
* @return the complete url
|
||||
*/
|
||||
public static String createLinkHeader(final String uri, final String rel) {
|
||||
return "<" + uri + ">; rel=\"" + rel + "\"";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.web.util;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import com.baeldung.web.exception.MyResourceNotFoundException;
|
||||
|
||||
/**
|
||||
* Simple static methods to be called at the start of your own methods to verify correct arguments and state. If the Precondition fails, an {@link HttpStatus} code is thrown
|
||||
*/
|
||||
public final class RestPreconditions {
|
||||
|
||||
private RestPreconditions() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
/**
|
||||
* Check if some value was found, otherwise throw exception.
|
||||
*
|
||||
* @param expression
|
||||
* has value true if found, otherwise false
|
||||
* @throws MyResourceNotFoundException
|
||||
* if expression is false, means value not found.
|
||||
*/
|
||||
public static void checkFound(final boolean expression) {
|
||||
if (!expression) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if some value was found, otherwise throw exception.
|
||||
*
|
||||
* @param expression
|
||||
* has value true if found, otherwise false
|
||||
* @throws MyResourceNotFoundException
|
||||
* if expression is false, means value not found.
|
||||
*/
|
||||
public static <T> T checkFound(final T resource) {
|
||||
if (resource == null) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
|
||||
|
||||
<!-- We have to exclude Spring Boot's WebMvcAutoConfiguration if we want the xml-configured message-converters to work properly -->
|
||||
<!-- <mvc:annotation-driven> -->
|
||||
<!-- <mvc:message-converters -->
|
||||
<!-- register-defaults="true"> -->
|
||||
|
||||
<!-- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> -->
|
||||
<!-- <property name="marshaller" ref="xstreamMarshaller" /> -->
|
||||
<!-- <property name="unmarshaller" ref="xstreamMarshaller" /> -->
|
||||
<!-- </bean> -->
|
||||
<!-- <bean -->
|
||||
<!-- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> -->
|
||||
<!-- </mvc:message-converters> -->
|
||||
<!-- </mvc:annotation-driven> -->
|
||||
|
||||
<!-- <bean id="xstreamMarshaller" -->
|
||||
<!-- class="org.springframework.oxm.xstream.XStreamMarshaller"> -->
|
||||
<!-- <property name="autodetectAnnotations" value="true" /> -->
|
||||
<!-- </bean> -->
|
||||
<!-- -->
|
||||
|
||||
<!-- Also, we can JUST add a HttpMessageConverter Bean to the existing Spring Boot Autoconfiguration: -->
|
||||
<!-- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> -->
|
||||
<!-- <property name="marshaller" ref="xstreamMarshaller" /> -->
|
||||
<!-- <property name="unmarshaller" ref="xstreamMarshaller" /> -->
|
||||
<!-- </bean> -->
|
||||
<!-- <bean id="xstreamMarshaller" -->
|
||||
<!-- class="org.springframework.oxm.xstream.XStreamMarshaller"> -->
|
||||
<!-- <property name="autodetectAnnotations" value="true" /> -->
|
||||
<!-- </bean> -->
|
||||
</beans>
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
<!-- NOTE: web.xml is not used in Spring Boot. This is just for guidance, showing how an Etag Filter would be implemented using XML-based configs -->
|
||||
|
||||
<!-- <?xml version="1.0" encoding="UTF-8"?> -->
|
||||
<!-- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" -->
|
||||
<!-- xsi:schemaLocation=" -->
|
||||
<!-- http://java.sun.com/xml/ns/javaee -->
|
||||
<!-- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" -->
|
||||
<!-- > -->
|
||||
|
||||
<!-- <filter> -->
|
||||
<!-- <filter-name>etagFilter</filter-name> -->
|
||||
<!-- <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> -->
|
||||
<!-- </filter> -->
|
||||
<!-- <filter-mapping> -->
|
||||
<!-- <filter-name>etagFilter</filter-name> -->
|
||||
<!-- <url-pattern>/*</url-pattern> -->
|
||||
<!-- </filter-mapping> -->
|
||||
<!-- </web-app> -->
|
||||
@@ -1,5 +0,0 @@
|
||||
server.servlet.context-path=/spring-boot-rest
|
||||
|
||||
### Spring Boot default error handling configurations
|
||||
#server.error.whitelabel.enabled=false
|
||||
#server.error.include-stacktrace=always
|
||||
@@ -1,22 +0,0 @@
|
||||
## jdbc.X
|
||||
#jdbc.driverClassName=com.mysql.jdbc.Driver
|
||||
#jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true
|
||||
#jdbc.user=tutorialuser
|
||||
#jdbc.pass=tutorialmy5ql
|
||||
#
|
||||
## hibernate.X
|
||||
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||
#hibernate.show_sql=false
|
||||
#hibernate.hbm2ddl.auto=create-drop
|
||||
|
||||
|
||||
# jdbc.X
|
||||
jdbc.driverClassName=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:security_permission;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
jdbc.user=sa
|
||||
jdbc.pass=
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=false
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
@@ -1,10 +0,0 @@
|
||||
# jdbc.X
|
||||
jdbc.driverClassName=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true
|
||||
jdbc.user=tutorialuser
|
||||
jdbc.pass=tutorialmy5ql
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
|
||||
hibernate.show_sql=false
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
public interface Consts {
|
||||
int APPLICATION_PORT = 8080;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {SpringBootRestApplication.class})
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
package com.baeldung.common.web;
|
||||
|
||||
import static com.baeldung.web.util.HTTPLinkHeaderUtil.extractURIByRel;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
public abstract class AbstractBasicLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
|
||||
|
||||
public AbstractBasicLiveTest(final Class<T> clazzToSet) {
|
||||
super(clazzToSet);
|
||||
}
|
||||
|
||||
// find - all - paginated
|
||||
|
||||
@Test
|
||||
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
|
||||
create();
|
||||
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=10");
|
||||
|
||||
assertThat(response.getStatusCode(), is(200));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
|
||||
final String url = getURL() + "?page=" + randomNumeric(5) + "&size=10";
|
||||
final Response response = RestAssured.get(url);
|
||||
|
||||
assertThat(response.getStatusCode(), is(404));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
||||
create();
|
||||
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=10");
|
||||
|
||||
assertFalse(response.body().as(List.class).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFirstPageOfResourcesAreRetrieved_thenSecondPageIsNext() {
|
||||
create();
|
||||
create();
|
||||
create();
|
||||
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=2");
|
||||
|
||||
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
|
||||
assertEquals(getURL() + "?page=1&size=2", uriToNextPage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFirstPageOfResourcesAreRetrieved_thenNoPreviousPage() {
|
||||
final Response response = RestAssured.get(getURL() + "?page=0&size=2");
|
||||
|
||||
final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev");
|
||||
assertNull(uriToPrevPage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSecondPageOfResourcesAreRetrieved_thenFirstPageIsPrevious() {
|
||||
create();
|
||||
create();
|
||||
|
||||
final Response response = RestAssured.get(getURL() + "?page=1&size=2");
|
||||
|
||||
final String uriToPrevPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "prev");
|
||||
assertEquals(getURL() + "?page=0&size=2", uriToPrevPage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLastPageOfResourcesIsRetrieved_thenNoNextPageIsDiscoverable() {
|
||||
create();
|
||||
create();
|
||||
create();
|
||||
|
||||
final Response first = RestAssured.get(getURL() + "?page=0&size=2");
|
||||
final String uriToLastPage = extractURIByRel(first.getHeader(HttpHeaders.LINK), "last");
|
||||
|
||||
final Response response = RestAssured.get(uriToLastPage);
|
||||
|
||||
final String uriToNextPage = extractURIByRel(response.getHeader(HttpHeaders.LINK), "next");
|
||||
assertNull(uriToNextPage);
|
||||
}
|
||||
|
||||
// etags
|
||||
|
||||
@Test
|
||||
public void givenResourceExists_whenRetrievingResource_thenEtagIsAlsoReturned() {
|
||||
// Given
|
||||
final String uriOfResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertNotNull(findOneResponse.getHeader(HttpHeaders.ETAG));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceWasRetrieved_whenRetrievingAgainWithEtag_thenNotModifiedReturned() {
|
||||
// Given
|
||||
final String uriOfResource = createAsUri();
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.get(uriOfResource);
|
||||
final String etagValue = findOneResponse.getHeader(HttpHeaders.ETAG);
|
||||
|
||||
// When
|
||||
final Response secondFindOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.headers("If-None-Match", etagValue)
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertTrue(secondFindOneResponse.getStatusCode() == 304);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceWasRetrievedThenModified_whenRetrievingAgainWithEtag_thenResourceIsReturned() {
|
||||
// Given
|
||||
final String uriOfResource = createAsUri();
|
||||
final Response firstFindOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.get(uriOfResource);
|
||||
final String etagValue = firstFindOneResponse.getHeader(HttpHeaders.ETAG);
|
||||
final long createdId = firstFindOneResponse.jsonPath().getLong("id");
|
||||
|
||||
Foo updatedFoo = new Foo("updated value");
|
||||
updatedFoo.setId(createdId);
|
||||
Response updatedResponse = RestAssured.given().contentType(ContentType.JSON).body(updatedFoo)
|
||||
.put(uriOfResource);
|
||||
assertThat(updatedResponse.getStatusCode() == 200);
|
||||
|
||||
// When
|
||||
final Response secondFindOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.headers("If-None-Match", etagValue)
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertTrue(secondFindOneResponse.getStatusCode() == 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Not Yet Implemented By Spring - https://jira.springsource.org/browse/SPR-10164")
|
||||
public void givenResourceExists_whenRetrievedWithIfMatchIncorrectEtag_then412IsReceived() {
|
||||
// Given
|
||||
final String uriOfResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response findOneResponse = RestAssured.given()
|
||||
.header("Accept", "application/json")
|
||||
.headers("If-Match", randomAlphabetic(8))
|
||||
.get(uriOfResource);
|
||||
|
||||
// Then
|
||||
assertTrue(findOneResponse.getStatusCode() == 412);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.baeldung.common.web;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.web.util.HTTPLinkHeaderUtil;
|
||||
import org.hamcrest.core.AnyOf;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> extends AbstractLiveTest<T> {
|
||||
|
||||
public AbstractDiscoverabilityLiveTest(final Class<T> clazzToSet) {
|
||||
super(clazzToSet);
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
// discoverability
|
||||
|
||||
@Test
|
||||
public void whenInvalidPOSTIsSentToValidURIOfResource_thenAllowHeaderListsTheAllowedActions() {
|
||||
// Given
|
||||
final String uriOfExistingResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response res = RestAssured.post(uriOfExistingResource);
|
||||
|
||||
// Then
|
||||
final String allowHeader = res.getHeader(HttpHeaders.ALLOW);
|
||||
assertThat(allowHeader, AnyOf.anyOf(containsString("GET"), containsString("PUT"), containsString("DELETE")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenResourceIsCreated_thenUriOfTheNewlyCreatedResourceIsDiscoverable() {
|
||||
// When
|
||||
final Foo newResource = new Foo(randomAlphabetic(6));
|
||||
final Response createResp = RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(newResource)
|
||||
.post(getURL());
|
||||
final String uriOfNewResource = createResp.getHeader(HttpHeaders.LOCATION);
|
||||
|
||||
// Then
|
||||
final Response response = RestAssured.given()
|
||||
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
|
||||
.get(uriOfNewResource);
|
||||
|
||||
final Foo resourceFromServer = response.body().as(Foo.class);
|
||||
assertThat(newResource, equalTo(resourceFromServer));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenResourceIsRetrieved_thenUriToGetAllResourcesIsDiscoverable() {
|
||||
// Given
|
||||
final String uriOfExistingResource = createAsUri();
|
||||
|
||||
// When
|
||||
final Response getResponse = RestAssured.get(uriOfExistingResource);
|
||||
|
||||
// Then
|
||||
final String uriToAllResources = HTTPLinkHeaderUtil.extractURIByRel(getResponse.getHeader("Link"), "collection");
|
||||
|
||||
final Response getAllResponse = RestAssured.get(uriToAllResources);
|
||||
assertThat(getAllResponse.getStatusCode(), is(200));
|
||||
}
|
||||
|
||||
// template method
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.baeldung.common.web;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import static com.baeldung.Consts.APPLICATION_PORT;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.baeldung.test.IMarshaller;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
public abstract class AbstractLiveTest<T extends Serializable> {
|
||||
|
||||
protected final Class<T> clazz;
|
||||
|
||||
@Autowired
|
||||
protected IMarshaller marshaller;
|
||||
|
||||
public AbstractLiveTest(final Class<T> clazzToSet) {
|
||||
super();
|
||||
|
||||
Preconditions.checkNotNull(clazzToSet);
|
||||
clazz = clazzToSet;
|
||||
}
|
||||
|
||||
// template method
|
||||
|
||||
public abstract void create();
|
||||
|
||||
public abstract String createAsUri();
|
||||
|
||||
protected final void create(final T resource) {
|
||||
createAsUri(resource);
|
||||
}
|
||||
|
||||
protected final String createAsUri(final T resource) {
|
||||
final Response response = createAsResponse(resource);
|
||||
Preconditions.checkState(response.getStatusCode() == 201, "create operation: " + response.getStatusCode());
|
||||
|
||||
final String locationOfCreatedResource = response.getHeader(HttpHeaders.LOCATION);
|
||||
Preconditions.checkNotNull(locationOfCreatedResource);
|
||||
return locationOfCreatedResource;
|
||||
}
|
||||
|
||||
final Response createAsResponse(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
|
||||
final String resourceAsString = marshaller.encode(resource);
|
||||
return RestAssured.given()
|
||||
.contentType(marshaller.getMime())
|
||||
.body(resourceAsString)
|
||||
.post(getURL());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
protected String getURL() {
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.SpringBootRestApplication;
|
||||
import com.baeldung.requestresponsebody.ExamplePostController;
|
||||
import com.baeldung.requestresponsebody.LoginForm;
|
||||
import com.baeldung.services.ExampleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootRestApplication.class)
|
||||
public class ExamplePostControllerRequestIntegrationTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
||||
@InjectMocks private ExamplePostController exampleController;
|
||||
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
private LoginForm lf = new LoginForm();
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(exampleController)
|
||||
.build();
|
||||
lf.setPassword("password");
|
||||
lf.setUsername("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestBodyTest() {
|
||||
try {
|
||||
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
|
||||
mockMvc
|
||||
.perform(post("/post/request")
|
||||
.content(jsonBody)
|
||||
.contentType("application/json"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.SpringBootRestApplication;
|
||||
import com.baeldung.requestresponsebody.ExamplePostController;
|
||||
import com.baeldung.requestresponsebody.LoginForm;
|
||||
import com.baeldung.services.ExampleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootRestApplication.class)
|
||||
public class ExamplePostControllerResponseIntegrationTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
||||
@InjectMocks private ExamplePostController exampleController;
|
||||
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
private LoginForm lf = new LoginForm();
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(exampleController)
|
||||
.build();
|
||||
lf.setPassword("password");
|
||||
lf.setUsername("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestBodyTest() {
|
||||
try {
|
||||
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
|
||||
mockMvc
|
||||
.perform(post("/post/response")
|
||||
.content(jsonBody)
|
||||
.contentType("application/json"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"text\":\"Thanks For Posting!!!\"}"));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.rest;
|
||||
|
||||
public class GitHubUser {
|
||||
|
||||
private String login;
|
||||
|
||||
public GitHubUser() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(final String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.baeldung.rest;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GithubBasicLiveTest {
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public void givenUserDoesNotExists_whenUserInfoIsRetrieved_then404IsReceived() throws ClientProtocolException, IOException {
|
||||
// Given
|
||||
final String name = randomAlphabetic(8);
|
||||
final HttpUriRequest request = new HttpGet("https://api.github.com/users/" + name);
|
||||
|
||||
// When
|
||||
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||
|
||||
// Then
|
||||
assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_NOT_FOUND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson() throws ClientProtocolException, IOException {
|
||||
// Given
|
||||
final String jsonMimeType = "application/json";
|
||||
final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp");
|
||||
|
||||
// When
|
||||
final HttpResponse response = HttpClientBuilder.create().build().execute(request);
|
||||
|
||||
// Then
|
||||
final String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
|
||||
assertEquals(jsonMimeType, mimeType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect() throws ClientProtocolException, IOException {
|
||||
// Given
|
||||
final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp");
|
||||
|
||||
// When
|
||||
final HttpResponse response = HttpClientBuilder.create().build().execute(request);
|
||||
|
||||
// Then
|
||||
final GitHubUser resource = RetrieveUtil.retrieveResourceFromResponse(response, GitHubUser.class);
|
||||
assertThat("eugenp", Matchers.is(resource.getLogin()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class RetrieveUtil {
|
||||
|
||||
// API
|
||||
|
||||
public static <T> T retrieveResourceFromResponse(final HttpResponse response, final Class<T> clazz) throws IOException {
|
||||
final String jsonFromResponse = EntityUtils.toString(response.getEntity());
|
||||
final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(jsonFromResponse, clazz);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.test")
|
||||
public class ConfigIntegrationTest implements WebMvcConfigurer {
|
||||
|
||||
public ConfigIntegrationTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.baeldung.springhateoas;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
import com.baeldung.services.CustomerService;
|
||||
import com.baeldung.services.OrderService;
|
||||
import com.baeldung.web.controller.CustomerController;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(CustomerController.class)
|
||||
public class CustomerControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@MockBean
|
||||
private CustomerService customerService;
|
||||
|
||||
@MockBean
|
||||
private OrderService orderService;
|
||||
|
||||
private static final String DEFAULT_CUSTOMER_ID = "customer1";
|
||||
private static final String DEFAULT_ORDER_ID = "order1";
|
||||
|
||||
@Test
|
||||
public void givenExistingCustomer_whenCustomerRequested_thenResourceRetrieved() throws Exception {
|
||||
given(this.customerService.getCustomerDetail(DEFAULT_CUSTOMER_ID))
|
||||
.willReturn(new Customer(DEFAULT_CUSTOMER_ID, "customerJohn", "companyOne"));
|
||||
|
||||
this.mvc.perform(get("/customers/" + DEFAULT_CUSTOMER_ID))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._links").doesNotExist())
|
||||
.andExpect(jsonPath("$.customerId", is(DEFAULT_CUSTOMER_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingOrder_whenOrderRequested_thenResourceRetrieved() throws Exception {
|
||||
given(this.orderService.getOrderByIdForCustomer(DEFAULT_CUSTOMER_ID, DEFAULT_ORDER_ID))
|
||||
.willReturn(new Order(DEFAULT_ORDER_ID, 1., 1));
|
||||
|
||||
this.mvc.perform(get("/customers/" + DEFAULT_CUSTOMER_ID + "/" + DEFAULT_ORDER_ID))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._links").doesNotExist())
|
||||
.andExpect(jsonPath("$.orderId", is(DEFAULT_ORDER_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingCustomerWithOrders_whenOrdersRequested_thenHalResourceRetrieved() throws Exception {
|
||||
Order order1 = new Order(DEFAULT_ORDER_ID, 1., 1);
|
||||
List<Order> orders = Collections.singletonList(order1);
|
||||
given(this.orderService.getAllOrdersForCustomer(DEFAULT_CUSTOMER_ID)).willReturn(orders);
|
||||
|
||||
this.mvc.perform(get("/customers/" + DEFAULT_CUSTOMER_ID + "/orders").accept(MediaTypes.HAL_JSON_VALUE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.orderList[0]._links.self.href",
|
||||
is("http://localhost/customers/customer1/order1")))
|
||||
.andExpect(jsonPath("$._links.self.href", is("http://localhost/customers/customer1/orders")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingCustomer_whenAllCustomersRequested_thenHalResourceRetrieved() throws Exception {
|
||||
// customers
|
||||
Customer retrievedCustomer = new Customer(DEFAULT_CUSTOMER_ID, "customerJohn", "companyOne");
|
||||
List<Customer> customers = Collections.singletonList(retrievedCustomer);
|
||||
given(this.customerService.allCustomers()).willReturn(customers);
|
||||
// orders
|
||||
Order order1 = new Order(DEFAULT_ORDER_ID, 1., 1);
|
||||
List<Order> orders = Collections.singletonList(order1);
|
||||
given(this.orderService.getAllOrdersForCustomer(DEFAULT_CUSTOMER_ID)).willReturn(orders);
|
||||
|
||||
this.mvc.perform(get("/customers/").accept(MediaTypes.HAL_JSON_VALUE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(
|
||||
jsonPath("$._embedded.customerList[0]._links.self.href", is("http://localhost/customers/customer1")))
|
||||
.andExpect(jsonPath("$._embedded.customerList[0]._links.allOrders.href",
|
||||
is("http://localhost/customers/customer1/orders")))
|
||||
.andExpect(jsonPath("$._links.self.href", is("http://localhost/customers")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.springpagination;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import org.junit.Test;
|
||||
import org.modelmapper.ModelMapper;
|
||||
|
||||
import com.baeldung.springpagination.dto.PostDto;
|
||||
import com.baeldung.springpagination.model.Post;
|
||||
|
||||
public class PostDtoUnitTest {
|
||||
|
||||
private ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
@Test
|
||||
public void whenConvertPostEntityToPostDto_thenCorrect() {
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle(randomAlphabetic(6));
|
||||
post.setUrl("www.test.com");
|
||||
|
||||
PostDto postDto = modelMapper.map(post, PostDto.class);
|
||||
assertEquals(post.getId(), postDto.getId());
|
||||
assertEquals(post.getTitle(), postDto.getTitle());
|
||||
assertEquals(post.getUrl(), postDto.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertPostDtoToPostEntity_thenCorrect() {
|
||||
PostDto postDto = new PostDto();
|
||||
postDto.setId(1L);
|
||||
postDto.setTitle(randomAlphabetic(6));
|
||||
postDto.setUrl("www.test.com");
|
||||
|
||||
Post post = modelMapper.map(postDto, Post.class);
|
||||
assertEquals(postDto.getId(), post.getId());
|
||||
assertEquals(postDto.getTitle(), post.getTitle());
|
||||
assertEquals(postDto.getUrl(), post.getUrl());
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IMarshaller {
|
||||
|
||||
<T> String encode(final T entity);
|
||||
|
||||
<T> T decode(final String entityAsString, final Class<T> clazz);
|
||||
|
||||
<T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz);
|
||||
|
||||
String getMime();
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public final class JacksonMarshaller implements IMarshaller {
|
||||
private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class);
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JacksonMarshaller() {
|
||||
super();
|
||||
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final <T> String encode(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
String entityAsJSON = null;
|
||||
try {
|
||||
entityAsJSON = objectMapper.writeValueAsString(resource);
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entityAsJSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourceAsString);
|
||||
|
||||
T entity = null;
|
||||
try {
|
||||
entity = objectMapper.readValue(resourceAsString, clazz);
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourcesAsString);
|
||||
|
||||
List<T> entities = null;
|
||||
try {
|
||||
if (clazz.equals(Foo.class)) {
|
||||
entities = objectMapper.readValue(resourcesAsString, new TypeReference<List<Foo>>() {
|
||||
// ...
|
||||
});
|
||||
} else {
|
||||
entities = objectMapper.readValue(resourcesAsString, List.class);
|
||||
}
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getMime() {
|
||||
return MediaType.APPLICATION_JSON.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Profile("test")
|
||||
public class TestMarshallerFactory implements FactoryBean<IMarshaller> {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public TestMarshallerFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public IMarshaller getObject() {
|
||||
final String testMime = env.getProperty("test.mime");
|
||||
if (testMime != null) {
|
||||
switch (testMime) {
|
||||
case "json":
|
||||
return new JacksonMarshaller();
|
||||
case "xml":
|
||||
return new XStreamMarshaller();
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
return new JacksonMarshaller();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<IMarshaller> getObjectType() {
|
||||
return IMarshaller.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public final class XStreamMarshaller implements IMarshaller {
|
||||
|
||||
private XStream xstream;
|
||||
|
||||
public XStreamMarshaller() {
|
||||
super();
|
||||
|
||||
xstream = new XStream();
|
||||
xstream.autodetectAnnotations(true);
|
||||
xstream.processAnnotations(Foo.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final <T> String encode(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
return xstream.toXML(resource);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourceAsString);
|
||||
return (T) xstream.fromXML(resourceAsString);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
|
||||
return this.decode(resourcesAsString, List.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getMime() {
|
||||
return MediaType.APPLICATION_XML.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
/**
|
||||
*
|
||||
* We'll start the whole context, but not the server. We'll mock the REST calls instead.
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class FooControllerAppIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception {
|
||||
this.mockMvc.perform(get("/foos").param("page", "0")
|
||||
.param("size", "2"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json("[]"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
public class FooControllerCustomEtagIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
private String FOOS_ENDPOINT = "/foos/";
|
||||
private String CUSTOM_ETAG_ENDPOINT_SUFFIX = "/custom-etag";
|
||||
|
||||
private static String serializeFoo(Foo foo) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper.writeValueAsString(foo);
|
||||
}
|
||||
|
||||
private static String createFooJson() throws Exception {
|
||||
return serializeFoo(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
private static Foo deserializeFoo(String fooJson) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper.readValue(fooJson, Foo.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceExists_whenRetrievingResourceUsingCustomEtagEndpoint_thenEtagIsAlsoReturned()
|
||||
throws Exception {
|
||||
// Given
|
||||
String createdResourceUri = this.mvc.perform(post(FOOS_ENDPOINT).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createFooJson()))
|
||||
.andExpect(status().isCreated())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getHeader(HttpHeaders.LOCATION);
|
||||
|
||||
// When
|
||||
ResultActions result = this.mvc
|
||||
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON));
|
||||
|
||||
// Then
|
||||
result.andExpect(status().isOk())
|
||||
.andExpect(header().string(HttpHeaders.ETAG, "\"0\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceWasRetrieved_whenRetrievingAgainWithEtagUsingCustomEtagEndpoint_thenNotModifiedReturned() throws Exception {
|
||||
// Given
|
||||
String createdResourceUri = this.mvc.perform(post(FOOS_ENDPOINT).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createFooJson()))
|
||||
.andExpect(status().isCreated())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getHeader(HttpHeaders.LOCATION);
|
||||
ResultActions findOneResponse = this.mvc
|
||||
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON));
|
||||
String etag = findOneResponse.andReturn().getResponse().getHeader(HttpHeaders.ETAG);
|
||||
|
||||
// When
|
||||
ResultActions result = this.mvc
|
||||
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON).header(HttpHeaders.IF_NONE_MATCH, etag));
|
||||
|
||||
// Then
|
||||
result.andExpect(status().isNotModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceWasRetrievedThenModified_whenRetrievingAgainWithEtagUsingCustomEtagEndpoint_thenResourceIsReturned() throws Exception {
|
||||
// Given
|
||||
String createdResourceUri = this.mvc.perform(post(FOOS_ENDPOINT).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(createFooJson()))
|
||||
.andExpect(status().isCreated())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getHeader(HttpHeaders.LOCATION);
|
||||
ResultActions findOneResponse = this.mvc
|
||||
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON));
|
||||
String etag = findOneResponse.andReturn().getResponse().getHeader(HttpHeaders.ETAG);
|
||||
Foo createdFoo = deserializeFoo(findOneResponse.andReturn().getResponse().getContentAsString());
|
||||
createdFoo.setName("updated name");
|
||||
this.mvc
|
||||
.perform(put(createdResourceUri).contentType(MediaType.APPLICATION_JSON).content(serializeFoo(createdFoo)));
|
||||
|
||||
// When
|
||||
ResultActions result = this.mvc
|
||||
.perform(get(createdResourceUri + CUSTOM_ETAG_ENDPOINT_SUFFIX).contentType(MediaType.APPLICATION_JSON).header(HttpHeaders.IF_NONE_MATCH, etag));
|
||||
|
||||
// Then
|
||||
result.andExpect(status().isOk())
|
||||
.andExpect(header().string(HttpHeaders.ETAG, "\"1\""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.service.IFooService;
|
||||
import com.baeldung.web.controller.FooController;
|
||||
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
* We'll start only the web layer.
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(FooController.class)
|
||||
public class FooControllerWebLayerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private IFooService service;
|
||||
|
||||
@MockBean
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
@Test()
|
||||
public void givenPresentFoo_whenFindPaginatedRequest_thenPageWithFooRetrieved() throws Exception {
|
||||
Page<Foo> page = new PageImpl<>(Collections.singletonList(new Foo("fooName")));
|
||||
when(service.findPaginated(0, 2)).thenReturn(page);
|
||||
doNothing().when(publisher)
|
||||
.publishEvent(any(PaginatedResultsRetrievedEvent.class));
|
||||
|
||||
this.mockMvc.perform(get("/foos").param("page", "0")
|
||||
.param("size", "2"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$",Matchers.hasSize(1)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import com.baeldung.common.web.AbstractDiscoverabilityLiveTest;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.spring.ConfigIntegrationTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ConfigIntegrationTest.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ActiveProfiles("test")
|
||||
public class FooDiscoverabilityLiveTest extends AbstractDiscoverabilityLiveTest<Foo> {
|
||||
|
||||
public FooDiscoverabilityLiveTest() {
|
||||
super(Foo.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final void create() {
|
||||
create(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String createAsUri() {
|
||||
return createAsUri(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.common.web.AbstractBasicLiveTest;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.spring.ConfigIntegrationTest;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ConfigIntegrationTest.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ActiveProfiles("test")
|
||||
public class FooLiveTest extends AbstractBasicLiveTest<Foo> {
|
||||
|
||||
public FooLiveTest() {
|
||||
super(Foo.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final void create() {
|
||||
create(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String createAsUri() {
|
||||
return createAsUri(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import static com.baeldung.Consts.APPLICATION_PORT;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.common.web.AbstractBasicLiveTest;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.spring.ConfigIntegrationTest;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ConfigIntegrationTest.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ActiveProfiles("test")
|
||||
public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
|
||||
|
||||
public FooPageableLiveTest() {
|
||||
super(Foo.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final void create() {
|
||||
super.create(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String createAsUri() {
|
||||
return createAsUri(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
|
||||
this.create();
|
||||
|
||||
final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
|
||||
|
||||
assertThat(response.getStatusCode(), is(200));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
|
||||
final String url = getPageableURL() + "?page=" + randomNumeric(5) + "&size=10";
|
||||
final Response response = RestAssured.get(url);
|
||||
|
||||
assertThat(response.getStatusCode(), is(404));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
||||
create();
|
||||
|
||||
final Response response = RestAssured.get(getPageableURL() + "?page=0&size=10");
|
||||
|
||||
assertFalse(response.body().as(List.class).isEmpty());
|
||||
}
|
||||
|
||||
protected String getPageableURL() {
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos/pageable";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.web.FooDiscoverabilityLiveTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
// @formatter:off
|
||||
FooDiscoverabilityLiveTest.class,
|
||||
FooLiveTest.class,
|
||||
FooPageableLiveTest.class
|
||||
}) //
|
||||
public class LiveTestSuiteLiveTest {
|
||||
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.web.server.MediaTypeNotSupportedStatusException;
|
||||
|
||||
import com.baeldung.web.controller.students.Student;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class StudentControllerIntegrationTest {
|
||||
|
||||
private static final String STUDENTS_PATH = "/students/";
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenReadAll_thenStatusIsOk() throws Exception {
|
||||
this.mockMvc.perform(get(STUDENTS_PATH))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadOne_thenStatusIsOk() throws Exception {
|
||||
this.mockMvc.perform(get(STUDENTS_PATH + 1))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreate_thenStatusIsCreated() throws Exception {
|
||||
Student student = new Student(10, "Albert", "Einstein");
|
||||
this.mockMvc.perform(post(STUDENTS_PATH).content(asJsonString(student))
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdate_thenStatusIsOk() throws Exception {
|
||||
Student student = new Student(1, "Nikola", "Tesla");
|
||||
this.mockMvc.perform(put(STUDENTS_PATH + 1)
|
||||
.content(asJsonString(student))
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDelete_thenStatusIsNoContent() throws Exception {
|
||||
this.mockMvc.perform(delete(STUDENTS_PATH + 3))
|
||||
.andExpect(status().isNoContent());
|
||||
}
|
||||
|
||||
private String asJsonString(final Object obj) {
|
||||
try {
|
||||
return new ObjectMapper().writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package com.baeldung.web.error;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.isA;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static com.baeldung.Consts.APPLICATION_PORT;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
public class ErrorHandlingLiveTest {
|
||||
|
||||
private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest";
|
||||
private static final String EXCEPTION_ENDPOINT = BASE_URL + "/exception";
|
||||
|
||||
private static final String ERROR_RESPONSE_KEY_PATH = "error";
|
||||
private static final String XML_RESPONSE_KEY_PATH = "xmlkey";
|
||||
private static final String LOCALE_RESPONSE_KEY_PATH = "locale";
|
||||
private static final String CAUSE_RESPONSE_KEY_PATH = "cause";
|
||||
private static final String RESPONSE_XML_ROOT = "Map";
|
||||
private static final String XML_RESPONSE_KEY_XML_PATH = RESPONSE_XML_ROOT + "." + XML_RESPONSE_KEY_PATH;
|
||||
private static final String LOCALE_RESPONSE_KEY_XML_PATH = RESPONSE_XML_ROOT + "." + LOCALE_RESPONSE_KEY_PATH;
|
||||
private static final String CAUSE_RESPONSE_KEY_XML_PATH = RESPONSE_XML_ROOT + "." + CAUSE_RESPONSE_KEY_PATH;
|
||||
private static final String CAUSE_RESPONSE_VALUE = "Error in the faulty controller!";
|
||||
private static final String XML_RESPONSE_VALUE = "the XML response is different!";
|
||||
|
||||
@Test
|
||||
public void whenRequestingFaultyEndpointAsJson_thenReceiveDefaultResponseWithConfiguredAttrs() {
|
||||
given().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
|
||||
.get(EXCEPTION_ENDPOINT)
|
||||
.then()
|
||||
.body("$", hasKey(LOCALE_RESPONSE_KEY_PATH))
|
||||
.body(CAUSE_RESPONSE_KEY_PATH, is(CAUSE_RESPONSE_VALUE))
|
||||
.body("$", not(hasKey(ERROR_RESPONSE_KEY_PATH)))
|
||||
.body("$", not(hasKey(XML_RESPONSE_KEY_PATH)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingFaultyEndpointAsXml_thenReceiveXmlResponseWithConfiguredAttrs() {
|
||||
given().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE)
|
||||
.get(EXCEPTION_ENDPOINT)
|
||||
.then()
|
||||
.body(LOCALE_RESPONSE_KEY_XML_PATH, isA(String.class))
|
||||
.body(CAUSE_RESPONSE_KEY_XML_PATH, is(CAUSE_RESPONSE_VALUE))
|
||||
.body(RESPONSE_XML_ROOT, not(hasKey(ERROR_RESPONSE_KEY_PATH)))
|
||||
.body(XML_RESPONSE_KEY_XML_PATH, is(XML_RESPONSE_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingFaultyEndpointAsHtml_thenReceiveWhitelabelPageResponse() throws Exception {
|
||||
try (WebClient webClient = new WebClient()) {
|
||||
webClient.getOptions()
|
||||
.setThrowExceptionOnFailingStatusCode(false);
|
||||
HtmlPage page = webClient.getPage(EXCEPTION_ENDPOINT);
|
||||
assertThat(page.getBody()
|
||||
.asText()).contains("Whitelabel Error Page");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public final class HTTPLinkHeaderUtil {
|
||||
|
||||
private HTTPLinkHeaderUtil() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static String extractURIByRel(final String linkHeader, final String rel) {
|
||||
if (linkHeader == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String uriWithSpecifiedRel = null;
|
||||
final String[] links = linkHeader.split(", ");
|
||||
String linkRelation;
|
||||
for (final String link : links) {
|
||||
final int positionOfSeparator = link.indexOf(';');
|
||||
linkRelation = link.substring(positionOfSeparator + 1, link.length()).trim();
|
||||
if (extractTypeOfRelation(linkRelation).equals(rel)) {
|
||||
uriWithSpecifiedRel = link.substring(1, positionOfSeparator - 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return uriWithSpecifiedRel;
|
||||
}
|
||||
|
||||
private static Object extractTypeOfRelation(final String linkRelation) {
|
||||
final int positionOfEquals = linkRelation.indexOf('=');
|
||||
return linkRelation.substring(positionOfEquals + 2, linkRelation.length() - 1).trim();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
{
|
||||
"info": {
|
||||
"_postman_id": "9989b5be-13ba-4d22-8e43-d05dbf628e58",
|
||||
"name": "foo API test",
|
||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
|
||||
},
|
||||
"item": [
|
||||
{
|
||||
"name": "add a foo",
|
||||
"event": [
|
||||
{
|
||||
"listen": "test",
|
||||
"script": {
|
||||
"id": "a01534dc-6fc7-4c54-ba1d-6bcf311e5836",
|
||||
"exec": [
|
||||
"pm.test(\"success status\", () => pm.response.to.be.success );",
|
||||
"",
|
||||
"pm.test(\"name is correct\", () => ",
|
||||
" pm.expect(pm.response.json().name).to.equal(\"Transformers\"));",
|
||||
"",
|
||||
"pm.test(\"id was assigned\", () => ",
|
||||
" pm.expect(pm.response.json().id).to.be.not.null );",
|
||||
"",
|
||||
"pm.variables.set(\"id\", pm.response.json().id);"
|
||||
],
|
||||
"type": "text/javascript"
|
||||
}
|
||||
}
|
||||
],
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"name": "Content-Type",
|
||||
"value": "application/json",
|
||||
"type": "text"
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"Transformers\"\n}"
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:8080/spring-boot-rest/foos",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"spring-boot-rest",
|
||||
"foos"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "get a foo",
|
||||
"event": [
|
||||
{
|
||||
"listen": "test",
|
||||
"script": {
|
||||
"id": "03de440c-b483-4ab8-a11a-d0c99b349963",
|
||||
"exec": [
|
||||
"pm.test(\"success status\", () => pm.response.to.be.success );",
|
||||
"",
|
||||
"pm.test(\"name is correct\", () => ",
|
||||
" pm.expect(pm.response.json().name).to.equal(\"Transformers\"));",
|
||||
"",
|
||||
"pm.test(\"id is correct\", () => ",
|
||||
" pm.expect(pm.response.json().id).to.equal(pm.variables.get(\"id\")) );"
|
||||
],
|
||||
"type": "text/javascript"
|
||||
}
|
||||
}
|
||||
],
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": ""
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"spring-boot-rest",
|
||||
"foos",
|
||||
"{{id}}"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "delete a foo",
|
||||
"event": [
|
||||
{
|
||||
"listen": "test",
|
||||
"script": {
|
||||
"id": "74c1bb0f-c06c-48b1-a545-459233541b14",
|
||||
"exec": [
|
||||
"pm.test(\"success status\", () => pm.response.to.be.success );"
|
||||
],
|
||||
"type": "text/javascript"
|
||||
}
|
||||
}
|
||||
],
|
||||
"request": {
|
||||
"method": "DELETE",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": ""
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"spring-boot-rest",
|
||||
"foos",
|
||||
"{{id}}"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "verify delete",
|
||||
"event": [
|
||||
{
|
||||
"listen": "test",
|
||||
"script": {
|
||||
"id": "03de440c-b483-4ab8-a11a-d0c99b349963",
|
||||
"exec": [
|
||||
"pm.test(\"status is 500\", () => pm.response.to.have.status(500) );",
|
||||
"",
|
||||
"pm.test(\"no value present\", () => ",
|
||||
" pm.expect(pm.response.json().cause).to.equal(\"No value present\"));"
|
||||
],
|
||||
"type": "text/javascript"
|
||||
}
|
||||
}
|
||||
],
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": ""
|
||||
},
|
||||
"url": {
|
||||
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
|
||||
"protocol": "http",
|
||||
"host": [
|
||||
"localhost"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"spring-boot-rest",
|
||||
"foos",
|
||||
"{{id}}"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user