small fixes to match articles
This commit is contained in:
parent
b7c1092460
commit
f62a8d0f70
|
@ -1,11 +1,13 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class SpringBootRestApplication {
|
public class SpringBootRestApplication {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SpringBootRestApplication.class, args);
|
SpringApplication.run(SpringBootRestApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ public interface IOperations<T extends Serializable> {
|
||||||
|
|
||||||
// read - one
|
// read - one
|
||||||
|
|
||||||
T findOne(final long id);
|
T findById(final long id);
|
||||||
|
|
||||||
// read - all
|
// read - all
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,8 @@ public abstract class AbstractService<T extends Serializable> implements IOperat
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public T findOne(final long id) {
|
public T findById(final long id) {
|
||||||
return getDao().findById(id)
|
return getDao().findById(id).orElse(null);
|
||||||
.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read - all
|
// read - all
|
||||||
|
|
|
@ -55,7 +55,7 @@ public class WebConfig implements WebMvcConfigurer {
|
||||||
@Bean
|
@Bean
|
||||||
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
|
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
|
||||||
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter());
|
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter());
|
||||||
filterRegistrationBean.addUrlPatterns("/auth/foos/*");
|
filterRegistrationBean.addUrlPatterns("/foos/*");
|
||||||
filterRegistrationBean.setName("etagFilter");
|
filterRegistrationBean.setName("etagFilter");
|
||||||
return filterRegistrationBean;
|
return filterRegistrationBean;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.util.List;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
import com.baeldung.persistence.model.Foo;
|
import com.baeldung.persistence.model.Foo;
|
||||||
|
@ -32,7 +34,7 @@ import com.baeldung.web.util.RestPreconditions;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/auth/foos")
|
@RequestMapping(value = "/foos")
|
||||||
public class FooController {
|
public class FooController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -40,6 +42,10 @@ public class FooController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IFooService service;
|
private IFooService service;
|
||||||
|
|
||||||
|
|
||||||
|
@Value("${version}")
|
||||||
|
Integer version;
|
||||||
|
|
||||||
public FooController() {
|
public FooController() {
|
||||||
super();
|
super();
|
||||||
|
@ -51,28 +57,36 @@ public class FooController {
|
||||||
@GetMapping(value = "/{id}/custom-etag")
|
@GetMapping(value = "/{id}/custom-etag")
|
||||||
public ResponseEntity<Foo> findByIdWithCustomEtag(@PathVariable("id") final Long id,
|
public ResponseEntity<Foo> findByIdWithCustomEtag(@PathVariable("id") final Long id,
|
||||||
final HttpServletResponse response) {
|
final HttpServletResponse response) {
|
||||||
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
|
final Foo foo = RestPreconditions.checkFound(service.findById(id));
|
||||||
|
|
||||||
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.eTag(Long.toString(resourceById.getVersion()))
|
.eTag(Long.toString(foo.getVersion()))
|
||||||
.body(resourceById);
|
.body(foo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// read - one
|
// read - one
|
||||||
|
|
||||||
@GetMapping(value = "/{id}")
|
@GetMapping(value = "/{id}")
|
||||||
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
|
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
|
||||||
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
|
||||||
return resourceById;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read - all
|
// read - all
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Foo> findAll() {
|
public List<Foo> findAll() {
|
||||||
|
System.out.println(version);
|
||||||
return service.findAll();
|
return service.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +134,7 @@ public class FooController {
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
|
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
|
||||||
Preconditions.checkNotNull(resource);
|
Preconditions.checkNotNull(resource);
|
||||||
RestPreconditions.checkFound(service.findOne(resource.getId()));
|
RestPreconditions.checkFound(service.findById(resource.getId()));
|
||||||
service.update(resource);
|
service.update(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,34 +7,28 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.util.UriTemplate;
|
import org.springframework.web.util.UriTemplate;
|
||||||
|
|
||||||
import com.baeldung.web.util.LinkUtil;
|
import com.baeldung.web.util.LinkUtil;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/auth/")
|
|
||||||
public class RootController {
|
public class RootController {
|
||||||
|
|
||||||
public RootController() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
// discover
|
// discover
|
||||||
|
|
||||||
@RequestMapping(value = "admin", method = RequestMethod.GET)
|
@GetMapping("/")
|
||||||
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
||||||
public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) {
|
public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) {
|
||||||
final String rootUri = request.getRequestURL()
|
final String rootUri = request.getRequestURL()
|
||||||
.toString();
|
.toString();
|
||||||
|
|
||||||
final URI fooUri = new UriTemplate("{rootUri}/{resource}").expand(rootUri, "foo");
|
final URI fooUri = new UriTemplate("{rootUri}{resource}").expand(rootUri, "foos");
|
||||||
final String linkToFoo = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection");
|
final String linkToFoos = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection");
|
||||||
response.addHeader("Link", linkToFoo);
|
response.addHeader("Link", linkToFoos);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
|
||||||
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
|
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
|
||||||
final String resourceName = clazz.getSimpleName()
|
final String resourceName = clazz.getSimpleName()
|
||||||
.toLowerCase() + "s";
|
.toLowerCase() + "s";
|
||||||
uriBuilder.path("/auth/" + resourceName);
|
uriBuilder.path("/" + resourceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
server.port=8082
|
|
||||||
server.servlet.context-path=/spring-boot-rest
|
server.servlet.context-path=/spring-boot-rest
|
||||||
|
|
||||||
### Spring Boot default error handling configurations
|
### Spring Boot default error handling configurations
|
||||||
#server.error.whitelabel.enabled=false
|
#server.error.whitelabel.enabled=false
|
||||||
#server.error.include-stacktrace=always
|
#server.error.include-stacktrace=always
|
||||||
|
|
||||||
|
version=1
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
public interface Consts {
|
public interface Consts {
|
||||||
int APPLICATION_PORT = 8082;
|
int APPLICATION_PORT = 8080;
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ public abstract class AbstractLiveTest<T extends Serializable> {
|
||||||
//
|
//
|
||||||
|
|
||||||
protected String getURL() {
|
protected String getURL() {
|
||||||
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/auth/foos";
|
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class FooControllerAppIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception {
|
public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception {
|
||||||
this.mockMvc.perform(get("/auth/foos").param("page", "0")
|
this.mockMvc.perform(get("/foos").param("page", "0")
|
||||||
.param("size", "2"))
|
.param("size", "2"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().json("[]"));
|
.andExpect(content().json("[]"));
|
||||||
|
|
|
@ -29,7 +29,7 @@ public class FooControllerCustomEtagIntegrationTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mvc;
|
private MockMvc mvc;
|
||||||
|
|
||||||
private String FOOS_ENDPOINT = "/auth/foos/";
|
private String FOOS_ENDPOINT = "/foos/";
|
||||||
private String CUSTOM_ETAG_ENDPOINT_SUFFIX = "/custom-etag";
|
private String CUSTOM_ETAG_ENDPOINT_SUFFIX = "/custom-etag";
|
||||||
|
|
||||||
private static String serializeFoo(Foo foo) throws Exception {
|
private static String serializeFoo(Foo foo) throws Exception {
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class FooControllerWebLayerIntegrationTest {
|
||||||
doNothing().when(publisher)
|
doNothing().when(publisher)
|
||||||
.publishEvent(any(PaginatedResultsRetrievedEvent.class));
|
.publishEvent(any(PaginatedResultsRetrievedEvent.class));
|
||||||
|
|
||||||
this.mockMvc.perform(get("/auth/foos").param("page", "0")
|
this.mockMvc.perform(get("/foos").param("page", "0")
|
||||||
.param("size", "2"))
|
.param("size", "2"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$",Matchers.hasSize(1)));
|
.andExpect(jsonPath("$",Matchers.hasSize(1)));
|
||||||
|
|
|
@ -74,7 +74,7 @@ public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getPageableURL() {
|
protected String getPageableURL() {
|
||||||
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/auth/foos/pageable";
|
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos/pageable";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,15 +42,14 @@
|
||||||
"raw": "{\n \"name\": \"Transformers\"\n}"
|
"raw": "{\n \"name\": \"Transformers\"\n}"
|
||||||
},
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": "http://localhost:8082/spring-boot-rest/auth/foos",
|
"raw": "http://localhost:8080/spring-boot-rest/foos",
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
"host": [
|
"host": [
|
||||||
"localhost"
|
"localhost"
|
||||||
],
|
],
|
||||||
"port": "8082",
|
"port": "8080",
|
||||||
"path": [
|
"path": [
|
||||||
"spring-boot-rest",
|
"spring-boot-rest",
|
||||||
"auth",
|
|
||||||
"foos"
|
"foos"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -85,15 +84,14 @@
|
||||||
"raw": ""
|
"raw": ""
|
||||||
},
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}",
|
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
"host": [
|
"host": [
|
||||||
"localhost"
|
"localhost"
|
||||||
],
|
],
|
||||||
"port": "8082",
|
"port": "8080",
|
||||||
"path": [
|
"path": [
|
||||||
"spring-boot-rest",
|
"spring-boot-rest",
|
||||||
"auth",
|
|
||||||
"foos",
|
"foos",
|
||||||
"{{id}}"
|
"{{id}}"
|
||||||
]
|
]
|
||||||
|
@ -123,15 +121,14 @@
|
||||||
"raw": ""
|
"raw": ""
|
||||||
},
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}",
|
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
"host": [
|
"host": [
|
||||||
"localhost"
|
"localhost"
|
||||||
],
|
],
|
||||||
"port": "8082",
|
"port": "8080",
|
||||||
"path": [
|
"path": [
|
||||||
"spring-boot-rest",
|
"spring-boot-rest",
|
||||||
"auth",
|
|
||||||
"foos",
|
"foos",
|
||||||
"{{id}}"
|
"{{id}}"
|
||||||
]
|
]
|
||||||
|
@ -164,15 +161,14 @@
|
||||||
"raw": ""
|
"raw": ""
|
||||||
},
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}",
|
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
"host": [
|
"host": [
|
||||||
"localhost"
|
"localhost"
|
||||||
],
|
],
|
||||||
"port": "8082",
|
"port": "8080",
|
||||||
"path": [
|
"path": [
|
||||||
"spring-boot-rest",
|
"spring-boot-rest",
|
||||||
"auth",
|
|
||||||
"foos",
|
"foos",
|
||||||
"{{id}}"
|
"{{id}}"
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue