2

I am saving a object using spring data in a mongo db. But i can not see the created id return with the json response object.

@JsonInclude(JsonInclude.Include.ALWAYS)
@ApiModel(description = "Submission")
public class Submission {

    @Id
    private String submissionId;
    private String assignmentId;
    private String title;

}

public interface SubmissionRepository extends MongoRepository<Submission, String>{

}

Below is what i do inside the controller,

Submission sub= submissionService.createSubmission(submission);
return new ResponseEntity<>(sub, HttpStatus.CREATED);

Even the sub object has the submissionId populate, but it does not comes with the response json object as a property. Any clue what only submissionId is missing from reponse?

Even with get request the id is not coming with the response. I am using spring boot environment with spring-data-mongodb-1.6.3

3
  • 1
    The ID in REST – as the name suggests – is the URI, hence the self link. Commented Sep 10, 2015 at 8:48
  • i didnt get your point Commented Sep 10, 2015 at 11:58
  • The client must not care about an artificially introduced property to satisfy a particular backend. Resources on the REST level are identified by their URIs, so this artificial backend identifier is omitted by default. Just like a version property is as it's translated into an ETag header, i.e. the protocol specific means to represent that property of the entity. Commented Sep 10, 2015 at 12:49

4 Answers 4

2

I found the solution for this by creating a config class as below,

@Configuration
public class RestDataConfig extends RepositoryRestMvcConfiguration{
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config)
    {
        super.configureRepositoryRestConfiguration(config);
        config.exposeIdsFor(Assignment.class);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

UPDATE

So Harshana has it right for Spring Boot 1.3.0 . You can just toss each class you want the id exposed for right under config.exposeIdsFor(Assignment.class). It worked for me now as well. So my answer below is just an alternative.

Also if you want HAL JSON to JSON API (aka Spring Boot Backend and Ember Data Frontend) Ember-Data-Hal-9000 has worked well for Ember Data 1.13.x .


The accepted answer did not work for me in Spring Boot. Instead I created an alias of id and used the Jackson annotation @JsonProperty("id")

import org.springframework.data.annotation.Id;
import com.fasterxml.jackson.annotation.JsonProperty;

 public class Calculation {

    @Id private String id;

    private String equation;
    private String result;

    @JsonProperty ("id")
    private String emberId;


    public String getEmberId() {
        return id;
    }

(since Ember Data needed one and using regex to pull it out of

_links: {
  self: {
    href: "http://localhost:4201/calculations/562796702b5e0940c66593fe"
   }
}

didn't sound like much fun in an Ember Adapter). Hope this helps somebody later.

Update looks like someone did put together a hal serializer, if you want to handle this in the client with Ember.

Comments

0

You can try having a public getId() in your Assignment.class

Comments

0

In my case I create a configuration class like this:

import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;

@Configuration
public class MongoConfiguration implements RepositoryRestConfigurer
{
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config)
    {
        config.exposeIdsFor(Brand.class);
    }
}

and it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.