JAVA-8363: Fix FooLiveTest - separate mime from etag code
This commit is contained in:
parent
8d8ae5daa9
commit
49ffde8b48
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.mime;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
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.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/foos")
|
||||||
|
public class FooController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FooDao fooDao;
|
||||||
|
|
||||||
|
@GetMapping(value = "/{id}")
|
||||||
|
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
|
||||||
|
return fooDao.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) {
|
||||||
|
return fooDao.save(resource);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.mime;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface FooDao extends CrudRepository<Foo, Long>{
|
||||||
|
}
|
@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.mime;
|
|
||||||
|
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.filter.ShallowEtagHeaderFilter;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class WebConfig {
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
// }
|
|
||||||
}
|
|
@ -18,7 +18,7 @@ import io.restassured.RestAssured;
|
|||||||
import io.restassured.response.Response;
|
import io.restassured.response.Response;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest(classes = WebConfig.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(classes = FooController.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
@ComponentScan({"com.baeldung.mime"})
|
@ComponentScan({"com.baeldung.mime"})
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
@ -44,12 +44,12 @@ public class FooLiveTest {
|
|||||||
createAsUri(resource);
|
createAsUri(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String createAsUri(final Foo resource) {
|
private String createAsUri(final Foo resource) {
|
||||||
final Response response = createAsResponse(resource);
|
final Response response = createAsResponse(resource);
|
||||||
return getURL() + "/" + response.getBody().as(Foo.class).getId();
|
return getURL() + "/" + response.getBody().as(Foo.class).getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Response createAsResponse(final Foo resource) {
|
private Response createAsResponse(final Foo resource) {
|
||||||
|
|
||||||
final String resourceAsString = marshaller.encode(resource);
|
final String resourceAsString = marshaller.encode(resource);
|
||||||
return RestAssured.given()
|
return RestAssured.given()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user