small fixes to match articles

This commit is contained in:
Loredana
2019-03-17 12:26:00 +02:00
parent b7c1092460
commit f62a8d0f70
15 changed files with 51 additions and 45 deletions
@@ -1,11 +1,13 @@
package com.baeldung;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestApplication.class, args);
}
@@ -9,7 +9,7 @@ public interface IOperations<T extends Serializable> {
// read - one
T findOne(final long id);
T findById(final long id);
// read - all
@@ -18,9 +18,8 @@ public abstract class AbstractService<T extends Serializable> implements IOperat
@Override
@Transactional(readOnly = true)
public T findOne(final long id) {
return getDao().findById(id)
.get();
public T findById(final long id) {
return getDao().findById(id).orElse(null);
}
// read - all
@@ -55,7 +55,7 @@ public class WebConfig implements WebMvcConfigurer {
@Bean
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter());
filterRegistrationBean.addUrlPatterns("/auth/foos/*");
filterRegistrationBean.addUrlPatterns("/foos/*");
filterRegistrationBean.setName("etagFilter");
return filterRegistrationBean;
}
@@ -5,6 +5,7 @@ import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
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.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;
@@ -32,7 +34,7 @@ import com.baeldung.web.util.RestPreconditions;
import com.google.common.base.Preconditions;
@RestController
@RequestMapping(value = "/auth/foos")
@RequestMapping(value = "/foos")
public class FooController {
@Autowired
@@ -40,6 +42,10 @@ public class FooController {
@Autowired
private IFooService service;
@Value("${version}")
Integer version;
public FooController() {
super();
@@ -51,28 +57,36 @@ public class FooController {
@GetMapping(value = "/{id}/custom-etag")
public ResponseEntity<Foo> findByIdWithCustomEtag(@PathVariable("id") final Long id,
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));
return ResponseEntity.ok()
.eTag(Long.toString(resourceById.getVersion()))
.body(resourceById);
.eTag(Long.toString(foo.getVersion()))
.body(foo);
}
// read - one
@GetMapping(value = "/{id}")
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
@GetMapping
public List<Foo> findAll() {
System.out.println(version);
return service.findAll();
}
@@ -120,7 +134,7 @@ public class FooController {
@ResponseStatus(HttpStatus.OK)
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
Preconditions.checkNotNull(resource);
RestPreconditions.checkFound(service.findOne(resource.getId()));
RestPreconditions.checkFound(service.findById(resource.getId()));
service.update(resource);
}
@@ -7,34 +7,28 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
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
@RequestMapping(value = "/auth/")
public class RootController {
public RootController() {
super();
}
// API
// discover
@RequestMapping(value = "admin", method = RequestMethod.GET)
@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, "foo");
final String linkToFoo = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection");
response.addHeader("Link", linkToFoo);
final URI fooUri = new UriTemplate("{rootUri}{resource}").expand(rootUri, "foos");
final String linkToFoos = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection");
response.addHeader("Link", linkToFoos);
}
}
@@ -115,7 +115,7 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
final String resourceName = clazz.getSimpleName()
.toLowerCase() + "s";
uriBuilder.path("/auth/" + resourceName);
uriBuilder.path("/" + resourceName);
}
}
@@ -1,6 +1,7 @@
server.port=8082
server.servlet.context-path=/spring-boot-rest
### Spring Boot default error handling configurations
#server.error.whitelabel.enabled=false
#server.error.include-stacktrace=always
#server.error.include-stacktrace=always
version=1