Merge pull request #6548 from eugenp/fix-boot-rest
small fixes to match articles
This commit is contained in:
commit
46c0ed9568
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,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 +33,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
|
||||
|
@ -51,22 +52,29 @@ 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
|
||||
|
@ -120,7 +128,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,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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package com.baeldung;
|
||||
|
||||
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() {
|
||||
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
|
||||
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("[]"));
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -51,7 +51,7 @@ public class FooControllerWebLayerIntegrationTest {
|
|||
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)));
|
||||
|
|
|
@ -74,7 +74,7 @@ public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> {
|
|||
}
|
||||
|
||||
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}"
|
||||
},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
|
@ -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}}"
|
||||
]
|
||||
|
@ -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}}"
|
||||
]
|
||||
|
@ -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}}"
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue