Skip to content

Commit 3ebce97

Browse files
author
eugenp
committed
- documentation work
1 parent 70ba3c1 commit 3ebce97

11 files changed

Lines changed: 546 additions & 80 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Sun Oct 23 00:05:59 EEST 2011
1+
#Sun Oct 23 22:19:23 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
@@ -50,7 +50,7 @@ org.eclipse.jdt.core.compiler.problem.redundantNullCheck=error
5050
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=error
5151
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=error
5252
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
53-
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=warning
53+
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
5454
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=enabled
5555
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=error
5656
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled

‎README.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ JSON payloads
8585

8686

8787

88-
## BLOG DISCUSSIONS
88+
## BLOG DISCUSSIONS (backed by this project)
8989
===================
9090

9191
* [How to avoid brittle tests when testing the Service Layer](http://www.baeldung.com/2011/10/02/testing-the-service-layer/)
9292
* [Introduction to Java integration testing for a REST API](http://www.baeldung.com/2011/10/13/integration-testing-a-rest-api/)
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/)
94+
* [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/)
95+
* [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/)
9496

9597

9698

@@ -99,9 +101,6 @@ JSON payloads
99101

100102
* improvements in validation and HTTP response codes
101103
* introduction of Spring Security to provide authentication and authorization
102-
* introduction of the cargo-maven-plugin to automate the deployment process and introduce remote deployment
103-
* starting an embedded Tomcat in the maven pre-integration-test phase and stopping it in the post-integration-test, so that the integration testing can be executed locally in the maven integration phase
104-
* using the Accept header to decide the type of the Representation - json is supported now, xml can be added later on
105104
* improving the marshaling of the server stack trace
106105
* 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
107106
* expand on the design document, covering: details of the transaction strategy

‎pom.xml‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
</dependency>
2323

2424
<!-- spring -->
25-
2625
<dependency>
2726
<groupId>org.springframework</groupId>
2827
<artifactId>spring-webmvc</artifactId>
@@ -41,15 +40,19 @@
4140
<scope>runtime</scope>
4241
</dependency>
4342

44-
<!-- json -->
43+
<!-- json and xml -->
4544
<dependency>
4645
<groupId>org.codehaus.jackson</groupId>
4746
<artifactId>jackson-mapper-asl</artifactId>
4847
<version>${jackson-mapper-asl.version}</version>
4948
<scope>runtime</scope>
5049
</dependency>
51-
<!-- <dependency> <groupId>xstream</groupId> <artifactId>xstream</artifactId> <version>${xstream.version}</version> <scope>runtime</scope>
52-
</dependency> -->
50+
<dependency>
51+
<groupId>javax.xml.bind</groupId>
52+
<artifactId>jaxb-api</artifactId>
53+
<version>${jaxb-api.version}</version>
54+
<scope>runtime</scope>
55+
</dependency>
5356

5457
<!-- persistence -->
5558
<dependency>
@@ -183,6 +186,13 @@
183186
<scope>test</scope>
184187
</dependency>
185188

189+
<dependency>
190+
<groupId>com.jayway.restassured</groupId>
191+
<artifactId>rest-assured</artifactId>
192+
<version>1.4</version>
193+
<scope>test</scope>
194+
</dependency>
195+
186196
</dependencies>
187197

188198
<build>
@@ -323,7 +333,7 @@
323333
<enabled>true</enabled>
324334
</snapshots>
325335
</repository>
326-
336+
327337
</repositories>
328338

329339
<properties>
@@ -340,6 +350,7 @@
340350

341351
<jackson-mapper-asl.version>1.8.5</jackson-mapper-asl.version>
342352
<xstream.version>1.2.2</xstream.version>
353+
<jaxb-api.version>2.2.4</jaxb-api.version>
343354

344355
<!-- VERSIONS - persistence -->
345356
<mysql-connector-java.version>5.1.17</mysql-connector-java.version> <!-- latest version on: 02.10.2011 - http://dev.mysql.com/downloads/connector/j/ -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.rest.controller.discovery;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
import org.springframework.web.bind.annotation.ResponseStatus;
9+
10+
/**
11+
* @author eugenp
12+
*/
13+
@Controller
14+
final class DiscoveryController{
15+
16+
public DiscoveryController(){
17+
super();
18+
}
19+
20+
// API
21+
22+
@RequestMapping( value = "",method = RequestMethod.GET )
23+
@ResponseBody
24+
@ResponseStatus( value = HttpStatus.NO_CONTENT )
25+
public final String root(){
26+
return "/foo";
27+
}
28+
29+
}

‎src/main/java/org/rest/model/Foo.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
import javax.persistence.GeneratedValue;
88
import javax.persistence.Id;
99
import javax.validation.constraints.NotNull;
10+
import javax.xml.bind.annotation.XmlRootElement;
1011

1112
import org.hibernate.annotations.GenericGenerator;
1213

1314
/**
1415
* @author eugenp
1516
*/
1617
@Entity
18+
@XmlRootElement
1719
public final class Foo implements Serializable{
1820
private static final long serialVersionUID = -2817129135033673162L;
1921

‎src/main/java/org/rest/spring/ApplicationConfig.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
@Configuration
1111
@ComponentScan( basePackages = "org.rest",excludeFilters = { @ComponentScan.Filter( Configuration.class ) } )
12+
// @PropertySource( { "classpath:persistence.properties", "classpath:restfull.properties" } )
1213
public class ApplicationConfig{
1314

1415
// API
@@ -21,5 +22,5 @@ public PropertyPlaceholderConfigurer properties(){
2122
ppc.setIgnoreUnresolvablePlaceholders( true );
2223
return ppc;
2324
}
24-
25+
2526
}

‎src/main/java/org/rest/spring/WebConfig.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
@Configuration
77
@EnableWebMvc
8-
/** since Jackson is on the classpath, this will automatically create and register a default JSON converter */
98
public class WebConfig{
10-
11-
// API
12-
9+
//
1310
}

‎src/test/java/org/rest/integration/FooRESTTemplate.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public final long createResource( final Foo resource ) throws ClientProtocolExce
5555
public final String getResourceAsJson( final long id ) throws ClientProtocolException, IOException{
5656
final HttpUriRequest request = new HttpGet( this.paths.getFooURL() + "/" + id );
5757
final HttpResponse response = new DefaultHttpClient().execute( request );
58-
final String jsonFromResponse = RetrieveUtil.retrieveJsonFromResponse( response );
58+
final String jsonFromResponse = RetrieveUtil.retrieveResourceFromResponse( response );
5959
return jsonFromResponse;
6060
}
61-
public final < T >T getResource( final long id, final Class< T > clazz ) throws ClientProtocolException, IOException{
61+
public final < T >T getResourceViaJson( final long id, final Class< T > clazz ) throws ClientProtocolException, IOException{
6262
final String resourceAsJson = this.getResourceAsJson( id );
6363
return ConvertUtil.convertJsonToResource( resourceAsJson, clazz );
6464
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.rest.integration;
2+
3+
import static com.jayway.restassured.RestAssured.given;
4+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
5+
6+
import java.io.IOException;
7+
8+
import org.apache.http.HttpHeaders;
9+
import org.apache.http.HttpResponse;
10+
import org.apache.http.client.ClientProtocolException;
11+
import org.apache.http.client.methods.HttpPost;
12+
import org.apache.http.impl.client.DefaultHttpClient;
13+
import org.rest.constants.HttpConstants;
14+
import org.rest.model.Foo;
15+
import org.rest.util.common.ExtractUtil;
16+
import org.rest.util.json.ConvertUtil;
17+
import org.rest.util.json.DecorateUtil;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.stereotype.Component;
20+
21+
import com.google.common.base.Preconditions;
22+
23+
/**
24+
* Template for the consumption of the REST API <br>
25+
*/
26+
@Component
27+
public final class FooRESTTemplateNew{
28+
29+
@Autowired
30+
ExamplePaths paths;
31+
32+
public FooRESTTemplateNew(){
33+
super();
34+
}
35+
36+
//
37+
public final long createResource() throws ClientProtocolException, IOException{
38+
return this.createResource( new Foo( randomAlphabetic( 6 ) ) );
39+
}
40+
public final long createResource( final Foo resource ) throws ClientProtocolException, IOException{
41+
Preconditions.checkNotNull( resource );
42+
43+
final HttpPost request = new HttpPost( this.paths.getFooURL() );
44+
DecorateUtil.setResourceOnRequestAsJson( request, resource );
45+
46+
final HttpResponse response = new DefaultHttpClient().execute( request );
47+
48+
final long idOfNewResource = ExtractUtil.extractIdFromCreateResponse( response );
49+
return idOfNewResource;
50+
}
51+
52+
/**
53+
* - note: usually, the
54+
*/
55+
public final String getResourceAsJson( final long id ){
56+
return given().header( HttpHeaders.ACCEPT, HttpConstants.MIME_JSON ).get( this.paths.getFooURL() + "/" + id ).asString();
57+
}
58+
public final < T >T getResourceViaJson( final long id, final Class< T > clazz ) throws ClientProtocolException, IOException{
59+
final String resourceAsJson = this.getResourceAsJson( id );
60+
return ConvertUtil.convertJsonToResource( resourceAsJson, clazz );
61+
}
62+
63+
}

0 commit comments

Comments
 (0)