Skip to content

Commit 362e89c

Browse files
author
eugenp
committed
- initial work on discoverability of the REST API
1 parent 75ba4eb commit 362e89c

30 files changed

Lines changed: 538 additions & 617 deletions

‎.project‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@
2525
<arguments>
2626
</arguments>
2727
</buildCommand>
28+
<buildCommand>
29+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
2833
</buildSpec>
2934
<natures>
35+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
3036
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
3137
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
3238
<nature>org.eclipse.m2e.core.maven2Nature</nature>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Sat Oct 29 15:33:06 EEST 2011
2+
eclipse.preferences.version=1
3+
encoding//src/main/resources/rebel.xml=UTF-8

‎.settings/org.eclipse.jdt.core.prefs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Fri Oct 28 22:01:04 EEST 2011
1+
#Sat Oct 29 14:01:54 EEST 2011
22
eclipse.preferences.version=1
33
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
44
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
@@ -12,7 +12,7 @@ org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
1212
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
1313
org.eclipse.jdt.core.compiler.problem.comparingIdentical=error
1414
org.eclipse.jdt.core.compiler.problem.deadCode=error
15-
org.eclipse.jdt.core.compiler.problem.deprecation=error
15+
org.eclipse.jdt.core.compiler.problem.deprecation=warning
1616
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=enabled
1717
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=enabled
1818
org.eclipse.jdt.core.compiler.problem.discouragedReference=error
@@ -70,8 +70,8 @@ org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCom
7070
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=enabled
7171
org.eclipse.jdt.core.compiler.problem.unusedImport=error
7272
org.eclipse.jdt.core.compiler.problem.unusedLabel=error
73-
org.eclipse.jdt.core.compiler.problem.unusedLocal=error
74-
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=error
73+
org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
74+
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
7575
org.eclipse.jdt.core.compiler.problem.unusedParameter=error
7676
org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=disabled
7777
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#Sat Oct 29 13:46:42 EEST 2011
2+
eclipse.preferences.version=1
3+
org.springframework.ide.eclipse.core.builders.enable.aopreferencemodelbuilder=false
4+
org.springframework.ide.eclipse.core.builders.enable.beanmetadatabuilder=true
5+
org.springframework.ide.eclipse.core.builders.enable.osgibundleupdater=false
6+
org.springframework.ide.eclipse.core.enable.project.preferences=false
7+
org.springframework.ide.eclipse.core.validator.enable.com.springsource.sts.bestpractices.beansvalidator=true
8+
org.springframework.ide.eclipse.core.validator.enable.org.springframework.ide.eclipse.beans.core.beansvalidator=true
9+
org.springframework.ide.eclipse.core.validator.enable.org.springframework.ide.eclipse.core.springvalidator=true
10+
org.springframework.ide.eclipse.core.validator.enable.org.springframework.ide.eclipse.webflow.core.validator=false
11+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.AvoidDriverManagerDataSource-com.springsource.sts.bestpractices.beansvalidator=true
12+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.ImportElementsAtTopRulee-com.springsource.sts.bestpractices.beansvalidator=true
13+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.ParentBeanSpecifiesAbstractClassRule-com.springsource.sts.bestpractices.beansvalidator=true
14+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.RefElementRule-com.springsource.sts.bestpractices.beansvalidator=true
15+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.TooManyBeansInFileRule-com.springsource.sts.bestpractices.beansvalidator=true
16+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.UnnecessaryValueElementRule-com.springsource.sts.bestpractices.beansvalidator=true
17+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.com.springsource.sts.bestpractices.UseBeanInheritance-com.springsource.sts.bestpractices.beansvalidator=true
18+
org.springframework.ide.eclipse.core.validator.rule.enable.com.springsource.sts.bestpractices.legacyxmlusage.jndiobjectfactory-com.springsource.sts.bestpractices.beansvalidator=true
19+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.autowire.autowire-org.springframework.ide.eclipse.beans.core.beansvalidator=true
20+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanAlias-org.springframework.ide.eclipse.beans.core.beansvalidator=true
21+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanClass-org.springframework.ide.eclipse.beans.core.beansvalidator=true
22+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanConstructorArgument-org.springframework.ide.eclipse.beans.core.beansvalidator=true
23+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanDefinition-org.springframework.ide.eclipse.beans.core.beansvalidator=true
24+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanDefinitionHolder-org.springframework.ide.eclipse.beans.core.beansvalidator=true
25+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanFactory-org.springframework.ide.eclipse.beans.core.beansvalidator=true
26+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanInitDestroyMethod-org.springframework.ide.eclipse.beans.core.beansvalidator=true
27+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanProperty-org.springframework.ide.eclipse.beans.core.beansvalidator=true
28+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.beanReference-org.springframework.ide.eclipse.beans.core.beansvalidator=true
29+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.methodOverride-org.springframework.ide.eclipse.beans.core.beansvalidator=true
30+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.parsingProblems-org.springframework.ide.eclipse.beans.core.beansvalidator=true
31+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.requiredProperty-org.springframework.ide.eclipse.beans.core.beansvalidator=true
32+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.beans.core.toolAnnotation-org.springframework.ide.eclipse.beans.core.beansvalidator=true
33+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.core.springClasspath-org.springframework.ide.eclipse.core.springvalidator=true
34+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.action-org.springframework.ide.eclipse.webflow.core.validator=false
35+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.actionstate-org.springframework.ide.eclipse.webflow.core.validator=false
36+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.attribute-org.springframework.ide.eclipse.webflow.core.validator=false
37+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.attributemapper-org.springframework.ide.eclipse.webflow.core.validator=false
38+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.beanaction-org.springframework.ide.eclipse.webflow.core.validator=false
39+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.evaluationaction-org.springframework.ide.eclipse.webflow.core.validator=false
40+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.evaluationresult-org.springframework.ide.eclipse.webflow.core.validator=false
41+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.exceptionhandler-org.springframework.ide.eclipse.webflow.core.validator=false
42+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.import-org.springframework.ide.eclipse.webflow.core.validator=false
43+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.inputattribute-org.springframework.ide.eclipse.webflow.core.validator=false
44+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.mapping-org.springframework.ide.eclipse.webflow.core.validator=false
45+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.outputattribute-org.springframework.ide.eclipse.webflow.core.validator=false
46+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.set-org.springframework.ide.eclipse.webflow.core.validator=false
47+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.state-org.springframework.ide.eclipse.webflow.core.validator=false
48+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.subflowstate-org.springframework.ide.eclipse.webflow.core.validator=false
49+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.transition-org.springframework.ide.eclipse.webflow.core.validator=false
50+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.variable-org.springframework.ide.eclipse.webflow.core.validator=false
51+
org.springframework.ide.eclipse.core.validator.rule.enable.org.springframework.ide.eclipse.webflow.core.validation.webflowstate-org.springframework.ide.eclipse.webflow.core.validator=false

‎.springBeans‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beansProjectDescription>
3+
<version>1</version>
4+
<pluginVersion><![CDATA[2.8.0.201110170010-RELEASE]]></pluginVersion>
5+
<configSuffixes>
6+
<configSuffix><![CDATA[xml]]></configSuffix>
7+
</configSuffixes>
8+
<enableImports><![CDATA[false]]></enableImports>
9+
<configs>
10+
<config>src/main/resources/spring-security-context.xml</config>
11+
</configs>
12+
<configSets>
13+
</configSets>
14+
</beansProjectDescription>

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ JSON payloads
9393
* [How to set up Integration Testing with the Maven Cargo plugin](http://www.baeldung.com/2011/10/16/how-to-set-up-integration-testing-with-the-maven-cargo-plugin/)
9494
* [Bootstrapping a web application with Spring 3.1 and Java based Configuration, part 1](http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/)
9595
* [Building a RESTful Web Service with Spring 3.1 and Java based Configuration, part 2](http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/)
96+
* [Securing a RESTful Web Service with Spring Security 3.1, part 3](http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/)
9697

9798

9899

99100
## FURTHER DEVELOPMENT
100101
======================
101102

102103
* improvements in validation and HTTP response codes
103-
* introduction of Spring Security to provide authentication and authorization
104104
* improving the marshaling of the server stack trace
105105
* improve the discoverability of the REST API - make good use of the Location HTTP header for returning full URLs (not just ids) so that the client doesn't have to construct his own URLs; introduce a /about URL to make discoverability better
106106
* expand on the design document, covering: details of the transaction strategy

‎pom.xml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
<dependency>
4545
<groupId>org.springframework.security</groupId>
4646
<artifactId>spring-security-web</artifactId>
47-
<version>3.1.0.CI-SNAPSHOT</version>
47+
<version>${spring-security.version}</version>
4848
</dependency>
4949
<dependency>
5050
<groupId>org.springframework.security</groupId>
5151
<artifactId>spring-security-config</artifactId>
52-
<version>3.1.0.CI-SNAPSHOT</version>
52+
<version>${spring-security.version}</version>
5353
</dependency>
5454
<dependency>
5555
<groupId>org.springframework</groupId>
@@ -274,6 +274,7 @@
274274
<plugin>
275275
<groupId>org.zeroturnaround</groupId>
276276
<artifactId>jrebel-maven-plugin</artifactId>
277+
<version>1.1.0</version>
277278
<executions>
278279
<execution>
279280
<id>generate-rebel-xml</id>
@@ -416,6 +417,7 @@
416417
<target>dev</target>
417418

418419
<!-- VERSIONS -->
420+
<spring-security.version>3.1.0.CI-SNAPSHOT</spring-security.version>
419421
<spring.version>3.1.0.BUILD-SNAPSHOT</spring.version>
420422
<javassist.version>3.15.0-GA</javassist.version>
421423
<cglib.version>2.2.2</cglib.version>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.rest.common.event;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.springframework.context.ApplicationListener;
7+
import org.springframework.stereotype.Component;
8+
9+
import com.google.common.base.Preconditions;
10+
11+
@Component
12+
public final class AddLinkHeaderListener implements ApplicationListener< EntityCreated >{
13+
14+
@Override
15+
public final void onApplicationEvent( final EntityCreated entityCreatedEvent ){
16+
Preconditions.checkNotNull( entityCreatedEvent );
17+
18+
final HttpServletRequest request = entityCreatedEvent.getRequest();
19+
final HttpServletResponse response = entityCreatedEvent.getResponse();
20+
final long idOfNewEntity = entityCreatedEvent.getIdOfNewEntity();
21+
22+
this.addLinkHeaderOnEntityCreation( request, response, idOfNewEntity );
23+
}
24+
25+
@SuppressWarnings( "unused" )
26+
final void addLinkHeaderOnEntityCreation( final HttpServletRequest request, final HttpServletResponse response, final long idOfNewEntity ){
27+
final String contextPath = request.getContextPath(); // /rest
28+
final String servletPath = request.getServletPath(); // /api
29+
final String pathInfo = request.getPathInfo(); // /admin/foo
30+
31+
final String requestURI = request.getRequestURI(); // /rest/api/admin/foo
32+
final StringBuffer requestURL = request.getRequestURL(); // http://localhost:8080/rest/api/admin/foo
33+
34+
response.addHeader( "Link", requestURL + "/" + idOfNewEntity );
35+
}
36+
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.rest.common.event;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.springframework.context.ApplicationEvent;
7+
8+
public class EntityCreated extends ApplicationEvent{
9+
private final HttpServletResponse response;
10+
private final long idOfNewEntity;
11+
private final HttpServletRequest request;
12+
13+
public EntityCreated( final Object sourceToSet, final HttpServletRequest requestToSet, final HttpServletResponse responseToSet, final long idOfNewEntityToSet ){
14+
super( sourceToSet );
15+
16+
this.request = requestToSet;
17+
this.response = responseToSet;
18+
this.idOfNewEntity = idOfNewEntityToSet;
19+
}
20+
21+
//
22+
23+
public final HttpServletRequest getRequest(){
24+
return this.request;
25+
}
26+
public final HttpServletResponse getResponse(){
27+
return this.response;
28+
}
29+
public final long getIdOfNewEntity(){
30+
return this.idOfNewEntity;
31+
}
32+
33+
}

‎src/main/java/org/rest/controller/foo/FooController.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import java.util.List;
44

5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
8+
import org.rest.common.event.EntityCreated;
59
import org.rest.common.util.RestPreconditions;
610
import org.rest.model.Foo;
711
import org.rest.service.foo.IFooService;
812
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.context.ApplicationContext;
914
import org.springframework.http.HttpStatus;
1015
import org.springframework.stereotype.Controller;
1116
import org.springframework.web.bind.annotation.PathVariable;
@@ -24,6 +29,9 @@ final class FooController{
2429
@Autowired
2530
IFooService service;
2631

32+
@Autowired
33+
ApplicationContext applicationContext;
34+
2735
public FooController(){
2836
super();
2937
}
@@ -45,9 +53,13 @@ public final Foo get( @PathVariable( "id" ) final Long id ){
4553
@RequestMapping( value = "admin/foo",method = RequestMethod.POST )
4654
@ResponseBody
4755
@ResponseStatus( HttpStatus.CREATED )
48-
public final Long create( @RequestBody final Foo entity ){
56+
public final Long create( @RequestBody final Foo entity, final HttpServletRequest request, final HttpServletResponse response ){
4957
RestPreconditions.checkNotNullFromRequest( entity );
50-
return this.service.create( entity );
58+
final Long idOfCreatedResource = this.service.create( entity );
59+
60+
this.applicationContext.publishEvent( new EntityCreated( this, request, response, idOfCreatedResource ) );
61+
62+
return idOfCreatedResource;
5163
}
5264

5365
@RequestMapping( value = "admin/foo",method = RequestMethod.PUT )

0 commit comments

Comments
 (0)