Skip to content

Instantly share code, notes, and snippets.

public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
@Controller
@RequestMapping("/employee")
public class EmployeeController {
private static ConcurrentHashMap<Long, Employee> employees = new ConcurrentHashMap<Long, Employee>();
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = { "application/json" })
public ResponseEntity<Employee> update(@RequestBody Employee employee, @PathVariable long id) {
if (employee.getId() != id) {
throw new IdNotMatchException();
}
employees.put(id, employee);
public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = { "application/json" })
@ResponseStatus(HttpStatus.OK)
public void update(@RequestBody Employee employee, @PathVariable long id) {
if (employee.getId() != id) {
throw new IdNotMatchException();
}
employees.put(id, employee);
}
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-mxj</artifactId>
<version>5.0.12</version>
<scope>test</scope>
</dependency>
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import com.mysql.management.MysqldResource;
public class EmbeddedMysqlDatabase extends DriverManagerDataSource {
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmbeddedDataSourceConfig implements DataSourceConfig {
@Override
@Bean(destroyMethod="shutdown")
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>