Skip to content

Commit

Permalink
Merge pull request eugenp#6548 from eugenp/fix-boot-rest
Browse files Browse the repository at this point in the history
small fixes to match articles
  • Loading branch information
lor6 authored Mar 17, 2019
2 parents b7c1092 + 7895993 commit 46c0ed9
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IOperations<T extends Serializable> {

// read - one

T findOne(final long id);
T findById(final long id);

// read - all

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -32,7 +33,7 @@
import com.google.common.base.Preconditions;

@RestController
@RequestMapping(value = "/auth/foos")
@RequestMapping(value = "/foos")
public class FooController {

@Autowired
Expand All @@ -51,22 +52,29 @@ public 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
Expand Down Expand Up @@ -120,7 +128,7 @@ public Foo create(@RequestBody final Foo resource, final HttpServletResponse res
@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,28 @@

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ final boolean hasLastPage(final int page, final int totalPages) {
protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) {
final String resourceName = clazz.getSimpleName()
.toLowerCase() + "s";
uriBuilder.path("/auth/" + resourceName);
uriBuilder.path("/" + resourceName);
}

}
3 changes: 1 addition & 2 deletions spring-boot-rest/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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
2 changes: 1 addition & 1 deletion spring-boot-rest/src/test/java/com/baeldung/Consts.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.baeldung;

public interface Consts {
int APPLICATION_PORT = 8082;
int APPLICATION_PORT = 8080;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final Response createAsResponse(final T resource) {
//

protected String getURL() {
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/auth/foos";
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FooControllerAppIntegrationTest {

@Test
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"))
.andExpect(status().isOk())
.andExpect(content().json("[]"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FooControllerCustomEtagIntegrationTest {
@Autowired
private MockMvc mvc;

private String FOOS_ENDPOINT = "/auth/foos/";
private String FOOS_ENDPOINT = "/foos/";
private String CUSTOM_ETAG_ENDPOINT_SUFFIX = "/custom-etag";

private static String serializeFoo(Foo foo) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void givenPresentFoo_whenFindPaginatedRequest_thenPageWithFooRetrieved()
doNothing().when(publisher)
.publishEvent(any(PaginatedResultsRetrievedEvent.class));

this.mockMvc.perform(get("/auth/foos").param("page", "0")
this.mockMvc.perform(get("/foos").param("page", "0")
.param("size", "2"))
.andExpect(status().isOk())
.andExpect(jsonPath("$",Matchers.hasSize(1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResourc
}

protected String getPageableURL() {
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/auth/foos/pageable";
return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos/pageable";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@
"raw": "{\n \"name\": \"Transformers\"\n}"
},
"url": {
"raw": "http://localhost:8082/spring-boot-rest/auth/foos",
"raw": "http://localhost:8080/spring-boot-rest/foos",
"protocol": "http",
"host": [
"localhost"
],
"port": "8082",
"port": "8080",
"path": [
"spring-boot-rest",
"auth",
"foos"
]
}
Expand Down Expand Up @@ -85,15 +84,14 @@
"raw": ""
},
"url": {
"raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}",
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
"protocol": "http",
"host": [
"localhost"
],
"port": "8082",
"port": "8080",
"path": [
"spring-boot-rest",
"auth",
"foos",
"{{id}}"
]
Expand Down Expand Up @@ -123,15 +121,14 @@
"raw": ""
},
"url": {
"raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}",
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
"protocol": "http",
"host": [
"localhost"
],
"port": "8082",
"port": "8080",
"path": [
"spring-boot-rest",
"auth",
"foos",
"{{id}}"
]
Expand Down Expand Up @@ -164,15 +161,14 @@
"raw": ""
},
"url": {
"raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}",
"raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}",
"protocol": "http",
"host": [
"localhost"
],
"port": "8082",
"port": "8080",
"path": [
"spring-boot-rest",
"auth",
"foos",
"{{id}}"
]
Expand Down

0 comments on commit 46c0ed9

Please sign in to comment.