Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3663

How to configure Spring Data REST to return the newly created entity as part of the body for a POST

$
0
0

I'm using Spring Data REST to expose my JPA repositories as Web Services.

I'm testing each of these Web Services using RestTemplate. I want those tests to be integration tests... meaning that I'm executing each test while my application is running. I don't want to mock anything.

Now what I want to know is how to configure Spring Data REST to return the newly created entity as part of the body when sending an HTTP POST.

I've tried to set the appropriate flags in configuration class RepositoryRestMvcConfiguration, but it doesn't seem to be working.

Here's my configuration class :

@Configurationpublic class RepositoryRestMvcConfiguration extends org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration {    @Override    public RepositoryRestConfiguration config() {        RepositoryRestConfiguration config = super.config();        config.setBasePath("/sdr-api");                        config.setReturnBodyOnCreate(Boolean.TRUE);        config.setReturnBodyOnUpdate(Boolean.TRUE);                config.exposeIdsFor(Project.class);                config.setDefaultMediaType(MediaType.APPLICATION_JSON);                return config;    }}

Here's an example of a test I wanna be able to perform :

@Test@SqlGroup({ @Sql(scripts = "/clear-data.sql", config = @SqlConfig(transactionMode = TransactionMode.ISOLATED)) })public void create() {    final String PROJECT_NAME = "MY_PROJECT";    Project project = new Project();    project.setName(PROJECT_NAME);    ResponseEntity<Project> response = restTemplate().postForEntity(getBaseUrl() +"projects", project, Project.class);    assertThat(response.getStatusCode(), equalTo(HttpStatus.CREATED));    Project projectCreated = response.getBody();    assertThat(projectCreated.getId(), notNullValue());    assertThat(projectCreated.getCreatedBy(), notNullValue());    assertThat(projectCreated.getCreatedDate(), notNullValue());    assertThat(projectCreated.getLastModifiedBy(), notNullValue());    assertThat(projectCreated.getLastModifiedDate(), notNullValue());    assertThat(projectCreated.getName(), equalTo(PROJECT_NAME));}

This test is failing on the following statement because the ID is null, as well as all the other properties...

assertThat(projectCreated.getId(), notNullValue());

Even when I try to call the WS using curl, it doesn't give the result I expect...

curl -i -X POST -H "Content-Type:application/json" -d '{ "name" : "MY_PROJECT" }' http://localhost:8081/myproject/sdr-api/projects

It should return the newly created entity and it should return the result as application/json and NOT application/hal+json as specified in my configuration class...

HTTP/1.1 201 CreatedServer: Apache-Coyote/1.1Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE, OPTIONSAccess-Control-Max-Age: 3600Access-Control-Allow-Headers: Accept, Origin, X-Requested-With, Content-Type, Last-Modified, AuthorizationAccess-Control-Allow-Credentials: trueETag: "0"Last-Modified: Tue, 10 Nov 2015 19:59:12 GMTLocation: http://localhost:8081/myproject/sdr-api/projects/4Content-Type: application/hal+jsonTransfer-Encoding: chunkedDate: Tue, 10 Nov 2015 19:59:12 GMT{"_links" : {"self" : {"href" : "http://localhost:8081/myproject/sdr-api/projects/4"    },"project" : {"href" : "http://localhost:8081/myproject/sdr-api/projects/4{?projection}","templated" : true    }  }}

Any idea on what the problem may be?

I've tried many things over the last few hours... with Spring 2.4.0.RELEASE, I've tried using the RepositoryRestConfigurerAdapter as suggested by

@Configuration@Import(RepositoryRestMvcConfiguration.class)public class SpringDataRestConfiguration extends RepositoryRestConfigurerAdapter {    @Override    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {        config.setBasePath("/sdr-api");        config.setReturnBodyOnCreate(Boolean.TRUE);        config.setReturnBodyOnUpdate(Boolean.TRUE);        config.exposeIdsFor(Project.class);        config.useHalAsDefaultJsonMediaType(false);        config.setDefaultMediaType(MediaType.APPLICATION_JSON);    }}

But I'm still getting the same result... the code seems to be executed though as my SDR API is available under /sdr-api.

So I've decided to downgrade the version of Spring Data Rest to 2.3.2.RELEASE. I've modified my configuration class this way :

@Configurationpublic class MySDRConfiguration extends RepositoryRestMvcConfiguration {    @Override    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {        config.setBasePath("/sdr-api");        config.setReturnBodyOnCreate(Boolean.TRUE);        config.setReturnBodyOnUpdate(Boolean.TRUE);        config.exposeIdsFor(Project.class);        config.useHalAsDefaultJsonMediaType(false);        config.setDefaultMediaType(MediaType.APPLICATION_JSON);    }}

And this time, it's working! The result I get is what I was expecting :

HTTP/1.1 201 CreatedServer: Apache-Coyote/1.1Access-Control-Allow-Methods: GET, POST, HEAD, PUT, DELETE, OPTIONSAccess-Control-Max-Age: 3600Access-Control-Allow-Headers: Accept, Origin, X-Requested-With, Content-Type, Last-Modified, AuthorizationAccess-Control-Allow-Credentials: trueETag: "0"Last-Modified: Thu, 12 Nov 2015 20:55:32 GMTLocation: http://localhost:8081/myproject/sdr-api/projects/3Content-Type: application/jsonTransfer-Encoding: chunkedDate: Thu, 12 Nov 2015 20:55:32 GMT{"id" : 3,"createdBy" : "anonymousUser","createdDate" : "2015-11-12T20:55:32.368Z","lastModifiedBy" : "anonymousUser","lastModifiedDate" : "2015-11-12T20:55:32.368Z","name" : "TEST","links" : [ ],"content" : [ ],"links" : [ {"rel" : "self","href" : "http://localhost:8081/myproject/sdr-api/projects/3"  }, {"rel" : "testPlans","href" : "http://localhost:8081/myproject/sdr-api/projects/3/testPlans"  } ]}

Am I missing something with the newest version of Spring Data REST?


Viewing all articles
Browse latest Browse all 3663

Trending Articles