Harshana
Harshana

Reputation: 7647

id missing in the json response with spring data in mongodb

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

Upvotes: 2

Views: 3653

Answers (4)

Kevin Montalvo
Kevin Montalvo

Reputation: 989

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

Upvotes: 0

Saurabh Jain
Saurabh Jain

Reputation: 31

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

Upvotes: 0

Eric D. Johnson
Eric D. Johnson

Reputation: 11747

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.

Upvotes: 0

Harshana
Harshana

Reputation: 7647

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);
    }
}

Upvotes: 2

Related Questions