-
-
Save jpukg/9e316e5679d492b690b9fc0ce3b32a99 to your computer and use it in GitHub Desktop.
Spring Boot RestFul Webservice Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html --- # | |
logging.level.org.springframework.web=DEBUG | |
logging.level.org.springframework.cache=DEBUG | |
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG | |
logging.level.org.springframework.security.web.authentication=DEBUG | |
logging.level.com.example.demo=TRACE | |
spring.mvc.dispatch-trace-request=true | |
spring.cache.ehcache.config=classpath:ehcache.xml | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.security; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* | |
* @author KMaji | |
* | |
*/ | |
public class AppUser{ | |
private String userName; | |
private String password; | |
private List<String> role; | |
public String[] getRole() { | |
if (null == role || role.isEmpty()) { | |
return null; | |
} | |
return role.toArray(new String[role.size()]); | |
} | |
public void setRole(List<String> role) { | |
this.role = role; | |
} | |
public String getUserName() { | |
return userName; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public AppUser(String userName, String password, String... roles) { | |
this.userName = userName; | |
this.password = password; | |
if (null != roles) | |
this.role = new ArrayList<>(); | |
for (String eachRole : roles) | |
role.add(eachRole); | |
} | |
@Override | |
public String toString() { | |
return "AppUser [userName=" + userName + ", password=" + password + ", role=" + role + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.app.configuration; | |
import org.springframework.cache.CacheManager; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.cache.concurrent.ConcurrentMapCacheManager; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@EnableCaching | |
@Configuration | |
public class CacheConfigurerAdapter { | |
/** | |
* Using simple spring cache. | |
* @return | |
*/ | |
// @Bean | |
CacheManager SimpleCacheManager() { | |
return new ConcurrentMapCacheManager("employeeCache"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.app.configuration; | |
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
@EnableWebMvc | |
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter { | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); | |
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.time.LocalDateTime; | |
import org.apache.log4j.Logger; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
@SpringBootApplication | |
// @ImportResource("classpath:cache-config.xml") // if we want to configure | |
// things in xml. | |
@ComponentScan(basePackages = { "com.example.demo", "com.example.demo.app.configuration", | |
"com.example.demo.application", "com.example.demo.application.aop", "com.example.demo.restinterface", | |
"com.example.demo.scheduler", "com.example.demo.security" }) | |
@EnableAutoConfiguration | |
public class DemoApplication { | |
private static final Logger LOG = Logger.getLogger(DemoApplication.class); | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("com.kaustuv.spring.example.boot.rest started at " + LocalDateTime.now()); | |
} | |
} | |
/** | |
* This class is responsible to redirect user to swagger ui so it will be easy | |
* for integrator or reader to understand what type of apis are published by | |
* this web app. | |
* | |
* @author KMaji | |
* | |
*/ | |
@Controller | |
class WelcomeController { | |
@RequestMapping(value = "/", method = RequestMethod.GET) | |
String home() { | |
return "redirect:/swagger-ui.html"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" | |
monitoring="autodetect" dynamicConfig="true"> | |
<diskStore path="java.io.tmpdir" /> | |
<!-- Read cache example --> | |
<cache name="employeeCache" | |
maxEntriesLocalHeap="10000" | |
maxEntriesLocalDisk="1000" | |
eternal="false" | |
diskSpoolBufferSizeMB="20" | |
timeToIdleSeconds="300" | |
timeToLiveSeconds="600" | |
memoryStoreEvictionPolicy="LFU" | |
transactionalMode="off" | |
clearOnFlush="true" | |
statistics="true" | |
logging="true"> | |
<persistence strategy="localTempSwap" /> | |
</cache> | |
</ehcache> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.domain; | |
import java.io.Serializable; | |
/** | |
* Employee domain class | |
* | |
* @author KMaji | |
* | |
*/ | |
public class Employee implements Serializable { | |
private static final long serialVersionUID = 7098286166079680079L; | |
public Employee(Integer id, String name, String department) { | |
super(); | |
this.id = id; | |
this.name = name; | |
this.department = department; | |
} | |
private Integer id; | |
private String name; | |
private String department; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getDepartment() { | |
return department; | |
} | |
public void setDepartment(String department) { | |
this.department = department; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((id == null) ? 0 : id.hashCode()); | |
result = prime * result + ((name == null) ? 0 : name.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Employee other = (Employee) obj; | |
if (id == null) { | |
if (other.id != null) | |
return false; | |
} else if (!id.equals(other.id)) | |
return false; | |
if (name == null) { | |
if (other.name != null) | |
return false; | |
} else if (!name.equals(other.name)) | |
return false; | |
return true; | |
} | |
@Override | |
public String toString() { | |
return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]"; | |
} | |
public Employee updateEmployee(String name2, String department2) { | |
this.department = department2; | |
this.name = name2; | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package com.example.demo.restinterface; | |
import java.util.Collection; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.MediaType; | |
import org.springframework.security.access.annotation.Secured; | |
import org.springframework.security.access.prepost.PreAuthorize; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.example.demo.domain.Employee; | |
import com.example.demo.domain.EmployeeService; | |
import io.swagger.annotations.Api; | |
import io.swagger.annotations.ApiOperation; | |
import io.swagger.annotations.Authorization; | |
/** | |
* Employee management services | |
* | |
* @author KMaji | |
* | |
*/ | |
@RequestMapping("/employee/services") | |
@Api(value = "/employee/services", description = "Employee management services", produces = MediaType.APPLICATION_JSON_VALUE) | |
@RestController() | |
public class EmployeeRestController { | |
@Autowired | |
EmployeeService employeeService; | |
@RequestMapping(value = "/listOfEmployee", method = RequestMethod.GET, produces = { | |
MediaType.APPLICATION_JSON_VALUE }) | |
@ResponseBody | |
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | |
@ApiOperation(value = "All employee details", notes = "Avialable employees", response = Employee.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = { | |
@Authorization(value = "basic"/* "security scope bounded to 'ROLE_USER' users " */) }) | |
Collection<Employee> getEmployees() { | |
return employeeService.getEmployees(); | |
} | |
@RequestMapping(value = "/employeeDetail", method = { org.springframework.web.bind.annotation.RequestMethod.GET }) | |
@ResponseBody | |
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | |
@ApiOperation(value = "Employee detail by id", notes = "Employee detail", response = Employee.class, authorizations = { | |
@Authorization(value = "security scope bounded to 'ROLE_ADMIN' users ") }) | |
Employee getEmployee(@RequestParam("id") Integer id) { | |
return employeeService.getEmployee(id); | |
} | |
@RequestMapping(value = "/addEmployee", method = { | |
org.springframework.web.bind.annotation.RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE) | |
@ResponseBody | |
@ApiOperation(value = "Add Employee", notes = "Employee add", response = Employee.class, produces = MediaType.APPLICATION_JSON_VALUE, authorizations = { | |
@Authorization(value = "security scope bounded to 'ROLE_ADMIN' users ") }) | |
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | |
Employee addEmployees(@RequestParam("id") Integer id, @RequestParam("name") String name, | |
@RequestParam(value = "department", required = false, defaultValue = "dev") String department) { | |
return employeeService.addEmployees(new Employee(id, name, department)); | |
} | |
@RequestMapping(value = "/deleteEmployee", method = { | |
org.springframework.web.bind.annotation.RequestMethod.DELETE }) | |
@ResponseBody | |
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | |
@ApiOperation(value = "Employee deletion", notes = "Employee delete", authorizations = { | |
@Authorization(value = "security scope bounded to 'ROLE_ADMIN' users ") }) | |
void deleteEmployee(@RequestParam("id") Integer id) { | |
/* return */ employeeService.deleteEmployee(id); | |
} | |
@RequestMapping(value = "/updateEmployee", method = { | |
org.springframework.web.bind.annotation.RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_VALUE) | |
@ResponseBody | |
// @Secured("ROLE_ADMIN") | |
@PreAuthorize("hasAuthority('ROLE_ADMIN')") | |
@ApiOperation(value = "Employee details update", notes = "present scope: Only department details updation", response = Employee.class, produces = MediaType.APPLICATION_JSON_VALUE, authorizations = { | |
@Authorization(value = "security scope bounded to 'ROLE_ADMIN' users ") }) | |
Employee updateEmployee(@RequestParam("id") Integer id, @RequestParam(value = "name", required = false) String name, | |
@RequestParam(value = "department", required = true) String department) { | |
return employeeService.updateEmployee(id, name, department); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.domain; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Employee domain services. | |
* | |
* @author KMaji | |
* | |
*/ | |
public interface EmployeeService { | |
Map<Integer, Employee> employees = new HashMap<>(); | |
Employee getEmployee(Integer id); | |
Employee addEmployees(Employee newEmp); | |
void deleteEmployee(Integer id); | |
Employee updateEmployee(Integer id, String name, String department); | |
Collection<Employee> getEmployees(); | |
default void populate(){ | |
employees.put(1, new Employee(1, "Josh", "dev")); | |
employees.put(2, new Employee(2, "Rev", "qa")); | |
employees.put(3, new Employee(3, "Kaustuv", "dev")); | |
employees.put(4, new Employee(4, "Sam", "Hr")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.application; | |
import java.util.Collection; | |
import org.springframework.cache.annotation.Cacheable; | |
import org.springframework.stereotype.Component; | |
import com.example.demo.application.aop.LogMethodExecution; | |
import com.example.demo.domain.Employee; | |
import com.example.demo.domain.EmployeeService; | |
/** | |
* This class is hold the actual implementation that domain services provided to | |
* other layer of domain driven design. Some of the methods are cachable. | |
* | |
* @author KMaji | |
*/ | |
@Component | |
public class EmployeeServiceImpl implements EmployeeService { | |
EmployeeServiceImpl() { | |
populate(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.example.demo.EmployeeService#getEmployee(java.lang.Integer) | |
*/ | |
@Cacheable(value = "employeeCache", key = "#id", sync = true) | |
@Override | |
@LogMethodExecution | |
public Employee getEmployee(Integer id) { | |
return employees.get(id); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.example.demo.EmployeeService#addEmployees(com.example.demo.Employee) | |
*/ | |
@Override | |
@LogMethodExecution | |
public Employee addEmployees(Employee newEmp) { | |
return employees.put(newEmp.getId(), newEmp); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.example.demo.EmployeeService#deleteEmployee(java.lang.Integer) | |
*/ | |
@LogMethodExecution | |
public void deleteEmployee(Integer id) { | |
employees.remove(id); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.example.demo.EmployeeService#updateEmployee(java.lang.Integer, | |
* java.lang.String, java.lang.String) | |
*/ | |
@LogMethodExecution | |
public Employee updateEmployee(Integer id, String name, String department) { | |
return employees.compute(id, (k, v) -> v.updateEmployee(name, department)); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.example.demo.EmployeeService#getEmployees() | |
*/ | |
@Cacheable(value = "employeeCache", sync = true) | |
@LogMethodExecution | |
public Collection<Employee> getEmployees() { | |
return employees.values(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.application.aop; | |
import org.apache.log4j.Logger; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
import org.aspectj.lang.Signature; | |
import org.aspectj.lang.annotation.Around; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.springframework.stereotype.Component; | |
/** | |
* This class is responsible to create aop aspect for this rest app. At present | |
* we demonstrate annotation based log framework. | |
* | |
* @author KMaji | |
* | |
*/ | |
@Aspect | |
@Component | |
public class LogAspect { | |
private static final Logger LOG = Logger.getLogger(LogAspect.class); | |
public LogAspect() { | |
} | |
/** | |
* This method log the execution timeline of business methods which are | |
* annotated with {@link LogMethodExecution} | |
* | |
* @param joinPoint | |
* this provides the entry point of this rest app. | |
* @return | |
* @throws Throwable | |
*/ | |
@Around("@annotation(LogMethodExecution)") | |
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { | |
Signature signature = joinPoint.getSignature(); | |
if (LOG.isDebugEnabled()) { | |
LOG.debug(joinPoint.getClass()); | |
LOG.debug("[---] ## > Entering " + signature.getDeclaringTypeName() + "::" + signature.getName() + "(...)"); | |
} | |
long start = System.currentTimeMillis(); | |
Object proceed = joinPoint.proceed(); | |
long executionTime = System.currentTimeMillis() - start; | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("[---] ## > Exiting " + signature.getDeclaringTypeName() + "::" + signature.getName() + "(...)"); | |
} | |
if (LOG.isTraceEnabled()) { | |
LOG.trace("## > " + signature.getName() + "(...) executed in " + executionTime + "ms"); | |
} | |
return proceed; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.restinterface; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.filter.CommonsRequestLoggingFilter; | |
/** | |
* http://www.baeldung.com/spring-http-logging | |
*/ | |
@Configuration | |
public class LoggingFilterConfig { | |
@Bean | |
public CommonsRequestLoggingFilter reqLogFilter() { | |
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); | |
filter.setIncludeQueryString(true); | |
filter.setIncludeClientInfo(true); | |
filter.setIncludePayload(true); | |
filter.setMaxPayloadLength(10000); | |
filter.setIncludeHeaders(false); | |
filter.setBeforeMessagePrefix("{ Request packet : "); | |
filter.setAfterMessageSuffix(" }"); | |
return filter; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.application.aop; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
/** | |
* This annotation is used to identify the loggable methods | |
* | |
* @author KMaji | |
* | |
*/ | |
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface LogMethodExecution { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.kaustuv.spring.example</groupId> | |
<artifactId>com.kaustuv.spring.example.boot.rest</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>com.kaustuv.spring.example.boot.rest</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.5.6.RELEASE</version> | |
<relativePath /> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-cache</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-autoconfigure</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-aop</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-security</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-logging</artifactId> | |
</dependency> | |
<!-- https://www.dontpanicblog.co.uk/2017/04/14/spring-boot-actuator-trace/ --> | |
<!-- https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-actuator</artifactId> | |
</dependency> | |
<!-- ehcache --> | |
<dependency> | |
<groupId>net.sf.ehcache</groupId> | |
<artifactId>ehcache</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>log4j</groupId> | |
<artifactId>log4j</artifactId> | |
<version>1.2.17</version> | |
</dependency> | |
<!-- swagger integration using spring fox --> | |
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-swagger2</artifactId> | |
<version>2.6.1</version> | |
</dependency> | |
<dependency> | |
<groupId>io.springfox</groupId> | |
<artifactId>springfox-swagger-ui</artifactId> | |
<version>2.6.1</version> | |
</dependency> | |
<!-- Test --> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.mockito</groupId> | |
<artifactId>mockito-core</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package com.example.demo.security; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.log4j.Logger; | |
import org.springframework.security.authentication.AuthenticationProvider; | |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.AuthenticationException; | |
import org.springframework.security.core.GrantedAuthority; | |
import org.springframework.security.core.authority.AuthorityUtils; | |
import org.springframework.security.core.userdetails.User; | |
import org.springframework.stereotype.Component; | |
/** | |
* This class provides the username password authentication implementation. | |
* | |
* @author KMaji | |
* | |
*/ | |
// @Configuration | |
@Component | |
public class RestAppAuthenticationProvider implements AuthenticationProvider { | |
private static final Logger LOG = Logger.getLogger(RestAppAuthenticationProvider.class); | |
Map<String, AppUser> users = new HashMap<>(); | |
private static final String ROLE_ADMIN = "ROLE_ADMIN"; | |
private static final String ROLE_USER = "ROLE_USER"; | |
/** | |
* | |
*/ | |
public RestAppAuthenticationProvider() { | |
users.put("kaustuv", new AppUser("kaustuv", "pass@123", ROLE_ADMIN, ROLE_USER)); | |
users.put("user", new AppUser("user", "user@123", ROLE_USER)); | |
users.put("admin", new AppUser("admin", "admin@123", ROLE_ADMIN)); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.springframework.security.authentication.AuthenticationProvider# | |
* authenticate(org.springframework.security.core.Authentication) | |
*/ | |
@Override | |
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | |
// -ve scenario 1 : authentication object should not be null. very rare case. | |
// mostly | |
// configuration issues | |
if (null == authentication) { | |
return null; | |
} | |
// -ve scenario 2 : authentication name and credentials should not be null. very | |
// rare | |
// case. mostly configuration issues | |
if (null == authentication.getName() && null == authentication.getCredentials()) { | |
return null; | |
} | |
String user = authentication.getName().trim(); | |
String password = authentication.getCredentials().toString().trim(); | |
AppUser appuser = users.get(user); | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("Security user detail { " + appuser + " }"); | |
} | |
String[] roles = appuser.getRole(); | |
// -ve scenario 3 : user with empty roles is not eligible to access roles based | |
// services :( | |
if (null == roles || 0 == roles.length) { | |
return null; | |
} | |
Collection<GrantedAuthority> grantedAuths = AuthorityUtils.createAuthorityList(roles); | |
return new UsernamePasswordAuthenticationToken(new User(user, password, grantedAuths), password, grantedAuths); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.springframework.security.authentication.AuthenticationProvider#supports( | |
* java.lang.Class) | |
*/ | |
@Override | |
public boolean supports(Class<?> authentication) { | |
// only username password plaintext token is used to secure this spring boot | |
// rest app. in this step we are registering the type of authentication this | |
// RestAppAuthenticationProvider class supports. | |
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.security; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.autoconfigure.security.SecurityProperties; | |
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.annotation.Order; | |
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | |
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
import org.springframework.security.core.session.SessionRegistry; | |
import org.springframework.security.core.session.SessionRegistryImpl; | |
import org.springframework.security.web.session.HttpSessionEventPublisher; | |
/** | |
* Scenario 1 : Roles based authentication using url Scenario 2 : Roles based | |
* authentication using method level Annotation | |
* | |
* @author KMaji | |
* | |
*/ | |
@Configuration | |
@EnableWebSecurity | |
/* | |
* Scenario 1 | |
* | |
* @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) | |
*/ | |
/* | |
* Scenario 2 | |
*/ | |
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, order = SecurityProperties.ACCESS_OVERRIDE_ORDER) | |
public class RestAppSecurity extends WebSecurityConfigurerAdapter { | |
@Autowired | |
RestAppAuthenticationProvider restAppAuthenticationProvider; | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.springframework.security.config.annotation.web.configuration. | |
* WebSecurityConfigurerAdapter#configure(org.springframework.security.config. | |
* annotation.authentication.builders.AuthenticationManagerBuilder) | |
*/ | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
/* | |
* Scenario 1. using inMemoryAuthentication | |
*/ | |
// auth.inMemoryAuthentication().withUser("admin").password("admin@123").roles("ADMIN"). | |
// // Admin user | |
// and().withUser("user1").password("user@123").roles("USER"). // local user | |
// and().withUser("kaustuv").password("pass@123").roles("USER", "ADMIN"); // | |
// Myself having both | |
/* | |
* Scenario 2. Example of AuthenticationProvider | |
*/ | |
auth.authenticationProvider(restAppAuthenticationProvider); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.springframework.security.config.annotation.web.configuration. | |
* WebSecurityConfigurerAdapter#configure(org.springframework.security.config. | |
* annotation.web.builders.HttpSecurity) | |
*/ | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
// Scenario 1. url path configuration. | |
// http.httpBasic().and().authorizeRequests().antMatchers("/employee/**").hasRole("ADMIN").antMatchers("/**") | |
// .hasRole("USER").and().csrf().disable().headers().frameOptions().disable(); | |
// Scenario 2. this configuration is for method level configuration. | |
http.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf().disable().headers() | |
.frameOptions().disable(); | |
// maximum session allowed session | |
http.sessionManagement().maximumSessions(5).sessionRegistry(sessionRegistry()); | |
} | |
@Bean | |
public SessionRegistry sessionRegistry() { | |
return new SessionRegistryImpl(); | |
} | |
@Bean | |
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() { | |
return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.scheduler; | |
import java.time.LocalDateTime; | |
import org.apache.log4j.Logger; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.scheduling.annotation.EnableScheduling; | |
import org.springframework.scheduling.annotation.Scheduled; | |
import com.example.demo.application.aop.LogMethodExecution; | |
@Configuration | |
@EnableScheduling | |
public class ScheduleJob { | |
private static final Logger LOG = Logger.getLogger(ScheduleJob.class); | |
@LogMethodExecution | |
@Scheduled(initialDelay = 1000, fixedRate = 10000) | |
public void run() { | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("Spring Boot Scheduling job example This message is from scheduled job -> :: " | |
+ LocalDateTime.now()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<con:soapui-project id="9abff8a3-c589-4503-8abe-89e63195f9b2" activeEnvironment="Default" name="REST Project 1" resourceRoot="" soapui-version="5.3.0" abortOnError="false" runType="SEQUENTIAL" xmlns:con="http://eviware.com/soapui/config"><con:settings/><con:interface xsi:type="con:RestService" id="9cf1ee69-8d58-4601-98d2-277f4f9543da" wadlVersion="http://wadl.dev.java.net/2009/02" name="http://localhost:8080" type="rest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:settings/><con:definitionCache type="TEXT" rootPart=""/><con:endpoints><con:endpoint>http://localhost:8080</con:endpoint><con:endpoint>https://springbootrestexample.herokuapp.com/</con:endpoint></con:endpoints><con:resource name="listOfEmployee" path="/employee/services/listOfEmployee" id="6753cf37-7ab8-4ebc-a48c-cb61a51d7be5"><con:settings/><con:parameters/><con:method name="Method 1" id="54c83d73-f266-4b33-a74a-c41c7717683d" method="GET"><con:settings/><con:parameters/><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element>Response</con:element></con:representation><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>401 403</con:status><con:params/><con:element xmlns:lis="http://localhost/services/listOfEmployee">lis:Fault</con:element></con:representation><con:request name="Request 1" id="fdbe0489-af40-4a77-ac66-a2fdc1a3b1e7" mediaType="application/json"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/listOfEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters/><con:parameterOrder/></con:request></con:method></con:resource><con:resource name="addEmployee" path="/employee/services/addEmployee" id="26a469ec-0000-468f-a1eb-55370d29857d"><con:settings/><con:parameters><con:parameter><con:name>id</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter><con:parameter><con:name>name</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter></con:parameters><con:method name="Method 1" id="21c6a200-e302-4fb7-a1e9-ff124fcd2247" method="POST"><con:settings/><con:parameters/><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Response</con:element></con:representation><con:representation type="REQUEST"><con:mediaType>application/json</con:mediaType><con:params/></con:representation><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>401 403</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Fault</con:element></con:representation><con:request name="Request 1" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:preemptive>false</con:preemptive><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters> | |
<con:entry key="id" value="10"/> | |
<con:entry key="name" value="netcracker"/> | |
</con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>name</con:entry></con:parameterOrder></con:request></con:method></con:resource><con:resource name="deleteEmployee" path="/employee/services/deleteEmployee" id="d21558cb-d59a-4fe2-903e-951de8c8f7cd"><con:settings/><con:parameters><con:parameter><con:name>id</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter></con:parameters><con:method name="Method 1" id="f701e3b1-a77c-4512-a4fc-89e762f8b836" method="DELETE"><con:settings/><con:parameters/><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>500 405 401</con:status><con:params/><con:element xmlns:del="http://localhost/services/deleteEmployee">del:Fault</con:element></con:representation><con:representation type="REQUEST"><con:mediaType>application/json</con:mediaType><con:params/></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>200</con:status><con:params/><con:element>data</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>200</con:status><con:params/><con:element>data</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>200</con:status><con:params/><con:element>data</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element xmlns:del="http://localhost/services/deleteEmployee">del:Response</con:element></con:representation><con:request name="Request 1" id="53887447-7176-4e9e-801b-d50f3cea9027" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/deleteEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><entry key="id" value="10" xmlns="http://eviware.com/soapui/config"/></con:parameters><con:parameterOrder><con:entry>id</con:entry></con:parameterOrder></con:request></con:method></con:resource><con:resource name="updateEmployee" path="/employee/services/updateEmployee" id="26a469ec-0000-468f-a1eb-55370d29857d"><con:settings/><con:parameters><con:parameter><con:name>id</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter><con:parameter><con:name>department</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter><con:parameter><con:name>name</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter></con:parameters><con:method name=""Null" as a Employee name" id="21c6a200-e302-4fb7-a1e9-ff124fcd2247" method="PUT"><con:settings/><con:parameters/><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Response</con:element></con:representation><con:representation type="REQUEST"><con:mediaType>application/json</con:mediaType><con:params/></con:representation><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>400 401 404 405</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Fault</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>0</con:status><con:params/><con:element>data</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>0</con:status><con:params/><con:element>data</con:element></con:representation><con:request name="Request 1" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters> | |
<con:entry key="id" value="1"/> | |
<con:entry key="department" value="dev"/> | |
<con:entry key="name" value="null"/> | |
</con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>department</con:entry><con:entry>name</con:entry></con:parameterOrder></con:request></con:method><con:method name="null as a Employee name" id="21c6a200-e302-4fb7-a1e9-ff124fcd2247" method="PUT"><con:settings/><con:parameters><con:parameter><con:name>id</con:name><con:value>1</con:value><con:style>QUERY</con:style><con:default>1</con:default></con:parameter><con:parameter><con:name>department</con:name><con:value/><con:style>QUERY</con:style><con:default/></con:parameter><con:parameter><con:name>name</con:name><con:value/><con:style>QUERY</con:style><con:default/></con:parameter></con:parameters><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Response</con:element></con:representation><con:representation type="REQUEST"><con:mediaType>application/json</con:mediaType><con:params/></con:representation><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>400 401 404 405</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Fault</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>0</con:status><con:params/><con:element>data</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>0</con:status><con:params/><con:element>data</con:element></con:representation><con:request name="Request 1" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><entry key="id" value="1" xmlns="http://eviware.com/soapui/config"/></con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>department</con:entry><con:entry>name</con:entry></con:parameterOrder></con:request></con:method><con:method name="Success Modify" id="21c6a200-e302-4fb7-a1e9-ff124fcd2247" method="PUT"><con:settings/><con:parameters><con:parameter><con:name>id</con:name><con:value>1</con:value><con:style>QUERY</con:style><con:default>1</con:default></con:parameter><con:parameter><con:name>department</con:name><con:value/><con:style>QUERY</con:style><con:default/></con:parameter><con:parameter><con:name>name</con:name><con:value/><con:style>QUERY</con:style><con:default/></con:parameter></con:parameters><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Response</con:element></con:representation><con:representation type="REQUEST"><con:mediaType>application/json</con:mediaType><con:params/></con:representation><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>400 401 404 405</con:status><con:params/><con:element xmlns:add="http://localhost/services/addEmployee">add:Fault</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>0</con:status><con:params/><con:element>data</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType xsi:nil="true"/><con:status>0</con:status><con:params/><con:element>data</con:element></con:representation><con:request name="Request 1" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters> | |
<con:entry key="id" value="1"/> | |
<con:entry key="department" value="dev"/> | |
<con:entry key="name" value="Joshua"/> | |
</con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>department</con:entry><con:entry>name</con:entry></con:parameterOrder></con:request></con:method></con:resource><con:resource name="employeeDetail" path="/employee/services/employeeDetail" id="4f64b8cb-9d4a-4263-8858-b1e38428854b"><con:settings/><con:parameters><con:parameter><con:name>id</con:name><con:value/><con:style>QUERY</con:style><con:default/><con:description xsi:nil="true"/></con:parameter></con:parameters><con:method name="Method 1" id="97e8d849-5197-4842-8e6e-17eb072348b9" method="GET"><con:settings/><con:parameters/><con:representation type="RESPONSE"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element xmlns:emp="http://localhost/employee/services/employeeDetail">emp:Response</con:element></con:representation><con:request name="Request 1" id="175b51ce-192a-405b-99c1-ac74b72ea11c" mediaType="application/json"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/employee/services/employeeDetail</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><entry key="id" value="1" xmlns="http://eviware.com/soapui/config"/></con:parameters><con:parameterOrder><con:entry>id</con:entry></con:parameterOrder></con:request></con:method></con:resource><con:resource name="trace" path="/trace" id="2ad152dd-31d1-421e-a474-962b3c1970cb"><con:settings/><con:parameters/><con:method name="Method 1" id="09d15c4f-f85a-4d03-93ee-27666e956b40" method="GET"><con:settings/><con:parameters/><con:representation type="FAULT"><con:mediaType>application/json;charset=UTF-8</con:mediaType><con:status>401</con:status><con:params/><con:element xmlns:trac="http://localhost/trace">trac:Fault</con:element></con:representation><con:representation type="RESPONSE"><con:mediaType>application/vnd.spring-boot.actuator.v1+json;charset=UTF-8</con:mediaType><con:status>200</con:status><con:params/><con:element>Response</con:element></con:representation><con:request name="Request 1" id="9338ff34-d431-4870-bd89-bb09eea2c318" mediaType="application/json"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/trace</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters/></con:request></con:method></con:resource></con:interface><con:testSuite id="84102cb7-9eff-449a-aa13-670a8c83489d" name="Employee RestServices TestSuite"><con:description>TestSuite generated for REST Service [http://localhost:8080]</con:description><con:settings/><con:runType>SEQUENTIAL</con:runType><con:testCase id="bfc9e681-4a93-4113-9d42-07ece8c9e047" failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="listOfEmployee TestCase" searchProperties="true"><con:description>TestCase generated for REST Resource [listOfEmployee] located at [/employee/services/listOfEmployee]</con:description><con:settings/><con:testStep type="restrequest" name="Request 1" id="c619ccfa-3a18-4f1f-bcae-ad33a2cbd954"><con:settings/><con:config service="http://localhost:8080" resourcePath="/employee/services/listOfEmployee" methodName="Method 1" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="Request 1" id="fdbe0489-af40-4a77-ac66-a2fdc1a3b1e7" mediaType="application/json"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/listOfEmployee</con:originalUri><con:credentials><con:username>user</con:username><con:password>user@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters/><con:parameterOrder/></con:restRequest></con:config></con:testStep><con:properties/></con:testCase><con:testCase id="9d48c32f-8287-4981-ae57-e8038c9d65f8" failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="addEmployee TestCase" searchProperties="true"><con:description>TestCase generated for REST Resource [addEmployee] located at [/employee/services/addEmployee]</con:description><con:settings/><con:testStep type="restrequest" name="Request 1" id="eb7c3364-a724-46d6-80b5-7312640dded9"><con:settings/><con:config service="http://localhost:8080" resourcePath="/employee/services/addEmployee" methodName="Method 1" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="Request 1" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters> | |
<con:entry key="id" value="10"/> | |
<con:entry key="name" value="netcracker"/> | |
</con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>name</con:entry></con:parameterOrder></con:restRequest></con:config></con:testStep><con:properties/></con:testCase><con:testCase id="d26d100f-4cce-4205-bc86-9fd1cfc2c91c" failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="deleteEmployee TestCase" searchProperties="true"><con:description>TestCase generated for REST Resource [deleteEmployee] located at [/employee/services/deleteEmployee]</con:description><con:settings/><con:testStep type="restrequest" name="Request 1" id="f30a5872-a044-42b5-be98-b4af9e580968"><con:settings/><con:config service="http://localhost:8080" resourcePath="/employee/services/deleteEmployee" methodName="Method 1" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="Request 1" id="53887447-7176-4e9e-801b-d50f3cea9027" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/deleteEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><entry key="id" value="10" xmlns="http://eviware.com/soapui/config"/></con:parameters><con:parameterOrder><con:entry>id</con:entry></con:parameterOrder></con:restRequest></con:config></con:testStep><con:properties/></con:testCase><con:testCase id="c0c16f3f-13e7-47a4-b316-ecfed843a17e" failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="updateEmployee TestCase" searchProperties="true"><con:description>TestCase generated for REST Resource [updateEmployee] located at [/employee/services/updateEmployee]</con:description><con:settings/><con:testStep type="restrequest" name="+ve scenario 1" id="fe1bfdaa-8ba6-4411-9d91-60b3e04fbf84"><con:settings/><con:config service="http://localhost:8080" resourcePath="/employee/services/updateEmployee" methodName=""Null" as a Employee name" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="Request 1" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters> | |
<con:entry key="id" value="1"/> | |
<con:entry key="department" value="dev"/> | |
<con:entry key="name" value=""/> | |
</con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>department</con:entry><con:entry>name</con:entry></con:parameterOrder></con:restRequest></con:config></con:testStep><con:testStep type="restrequest" name="-ve scenario 1" id="76a16f2f-ede4-44bf-9a27-06f615555685"><con:settings/><con:config service="http://localhost:8080" methodName=""Null" as a Employee name" resourcePath="/employee/services/updateEmployee" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="REST Request" id="96585dd3-fd24-40f0-b327-fe0650439e79" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:encoding>UTF-8</con:encoding><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/employee/services/updateEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters> | |
<con:entry key="id" value="1"/> | |
<con:entry key="department" value="qa"/> | |
</con:parameters></con:restRequest></con:config></con:testStep><con:testStep type="restrequest" name="-ve scenario 2" id="905e92da-f9bd-43cc-9de2-491b203e5540"><con:settings/><con:config service="http://localhost:8080" resourcePath="/employee/services/updateEmployee" methodName="null as a Employee name" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="REST Request" id="388ccf5e-fabc-4f7d-8aff-369db48ef731" mediaType="application/json" postQueryString="false"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/services/addEmployee</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><entry key="id" value="1" xmlns="http://eviware.com/soapui/config"/></con:parameters><con:parameterOrder><con:entry>id</con:entry><con:entry>department</con:entry><con:entry>name</con:entry></con:parameterOrder></con:restRequest></con:config></con:testStep><con:properties/></con:testCase><con:testCase id="85881899-2248-4bb0-ae33-ff216115db0e" failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="employeeDetail TestCase" searchProperties="true"><con:description>TestCase generated for REST Resource [employeeDetail] located at [/employee/services/employeeDetail]</con:description><con:settings/><con:testStep type="restrequest" name="Request 1" id="e371041b-3979-41a2-ba43-8d51f05fa4b1"><con:settings/><con:config service="http://localhost:8080" resourcePath="/employee/services/employeeDetail" methodName="Method 1" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:restRequest name="Request 1" id="175b51ce-192a-405b-99c1-ac74b72ea11c" mediaType="application/json"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting></con:settings><con:endpoint>http://localhost:8080</con:endpoint><con:request/><con:originalUri>http://localhost/employee/services/employeeDetail</con:originalUri><con:credentials><con:username>kaustuv</con:username><con:password>pass@123</con:password><con:selectedAuthProfile>Basic</con:selectedAuthProfile><con:addedBasicAuthenticationTypes>Basic</con:addedBasicAuthenticationTypes><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><entry key="id" value="1" xmlns="http://eviware.com/soapui/config"/></con:parameters><con:parameterOrder><con:entry>id</con:entry></con:parameterOrder></con:restRequest></con:config></con:testStep><con:properties/></con:testCase><con:properties/></con:testSuite><con:properties/><con:wssContainer/><con:oAuth2ProfileContainer/><con:oAuth1ProfileContainer/><con:sensitiveInformation/></con:soapui-project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.restinterface; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import springfox.documentation.builders.RequestHandlerSelectors; | |
import springfox.documentation.service.ApiInfo; | |
import springfox.documentation.spi.DocumentationType; | |
import springfox.documentation.spring.web.plugins.Docket; | |
import springfox.documentation.swagger2.annotations.EnableSwagger2; | |
import static springfox.documentation.builders.PathSelectors.regex; | |
@Configuration | |
@EnableSwagger2 | |
public class Swagger2Config { | |
@Bean | |
public Docket api() { | |
return new Docket(DocumentationType.SWAGGER_2).select() | |
.apis(RequestHandlerSelectors.basePackage("com.example.demo")).paths(regex("/employee.*")).build() | |
.apiInfo(apiInfo()); | |
} | |
private ApiInfo apiInfo() { | |
ApiInfo apiInfo = new ApiInfo("Spring Boot Rest App Example", | |
"This is an spring boot example rest app. Role based secruity credentials " | |
+ " [user/user@123{Role: USER}], " | |
+ " [admin/admin@123{Role: ADMIN}], " | |
+ " [kaustuv/pass@123 {Role: ADMIN,USER }] " | |
+ " Click list Operations tab to explore operations. " + "Enabled eh-cahce for read services." | |
+ " Used AOP @around to evaluate the each method execution time this will be useful " | |
+ "during performance test evaluation." + "\n", | |
"1.0.0", "free for learning no terms", | |
new springfox.documentation.service.Contact("Kaustuv Maji", "http://kaustuvmaji.blogspot.in/", | |
"kaustuv.maji@gmail.com"), | |
"http://kaustuvmaji.blogspot.in/", "http://kaustuvmaji.blogspot.in/"); | |
return apiInfo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment