overall cleanup

This commit is contained in:
Eugen 2013-05-27 18:47:41 +03:00
parent af424e266b
commit b0692e8ef3
10 changed files with 53 additions and 68 deletions

View File

@ -88,6 +88,14 @@
<scope>runtime</scope>
</dependency>
<!-- util -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
</dependency>
<!-- test scoped -->
<dependency>

View File

@ -1,19 +1,37 @@
package org.baeldung.web.controller;
import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.baeldung.persistence.service.FooService;
import org.baeldung.web.dto.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
import com.google.common.base.Preconditions;
@Controller
@RequestMapping(value = "/foo")
public class FooController {
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired
private FooService service;
public FooController() {
super();
}
@ -26,4 +44,31 @@ public class FooController {
return new Foo();
}
@RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET)
@ResponseBody
public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
final Foo resourceById = Preconditions.checkNotNull(service.getById(id));
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
return resourceById;
}
@RequestMapping(value = "admin/foo", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody final Foo resource, final HttpServletRequest request, final HttpServletResponse response) {
Preconditions.checkNotNull(resource);
final Long idOfCreatedResource = service.create(resource);
eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource));
}
@RequestMapping(value = "admin", method = RequestMethod.GET)
@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);
}
}

View File

@ -1,63 +0,0 @@
package org.baeldung.web.controller;
import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.baeldung.persistence.service.FooService;
import org.baeldung.web.dto.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.util.UriTemplate;
import com.google.common.base.Preconditions;
@Controller
public class FooController {
@Autowired
private FooService service;
@Autowired
private ApplicationEventPublisher eventPublisher;
// API
@RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET)
@ResponseBody
public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
final Foo resourceById = Preconditions.checkNotNull(service.getById(id));
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
return resourceById;
}
@RequestMapping(value = "admin/foo", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody final Foo resource, final HttpServletRequest request, final HttpServletResponse response) {
Preconditions.checkNotNull(resource);
final Long idOfCreatedResource = service.create(resource);
eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource));
}
@RequestMapping(value = "admin", method = RequestMethod.GET)
@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);
}
}

View File

@ -1,5 +0,0 @@
package org.baeldung.web.dto;
public class Foo {
}