1

can any one please tell me what I am missing ? I am trying to create a Mongo collection using spring boot mongodb.

I want to create something like this

{
    "_id":"123456"
    "entity_name":"some name"
    "entity_desc":"some description"
    "events":[
                {"title":"some title", "description":"some description"},
                {"title":"title2", "description":"description2" }
             ]
}

but I am getting this

{
   "_id":"123456"
"entity_name":"some name"
"entity_desc":"some description"
"events":[ ]
}

my Domain classes are

@Document
public class Entity {

     @Id
     private BigInteger id;

     private String name, description;

     private List<Event> events=new ArrayList<Event>();

    /* GETTERS AND SETTERS */

} 




public class Event {

    private String titles;
    private String descriptions;

    /* GETTERS AND SETTERS */

}

and my repository is

public interface MyRepository extends MongoRepository<Entity, String>{

}

controller is

@Controller
public class RootController {

    @Autowired
    private MyRepository mr;

    /* GET and other methods */


      @RequestMapping(value="/", method=RequestMethod.POST)
      public String helloPost(@ModelAttribute Entity entity){



          mr.save(entity);

          return "success";

     }
}

and my jsp form is

<form:form modelAttribute="entity" role="form">


            <div class="form-group">
             Name <input type="text" id="name" name="name" /><br />
             </div>
             <div class="form-group">
        Description <input type="text" id="description"   name="description" /><br />
        </div>

      <div class="form-group">
        event <input type="text" id="event" name="event" /><br />
        </div>

      <div class="form-group" >
        title <input type="text" id="title" name="title" /><br />
        </div>

        <div class="form-group" >
        description <input type="text" id="description" name="description" /><br />
       </div>


        <div>
        <button type="submit" class="btn btn-default">Submit</button>
        </div>



    </form:form>

1 Answer 1

0

Shouldn't it be something like below?

public class Event {

    private String title;
    private String description;

    /* GETTERS AND SETTERS */

}

EDIT : Thanks for the jsp, clearly the way you are passing the values are not picked for child. Spring MVC doesn't support ONGL so it won't pick up child object with dot notation. But there are complex mechanism by setting path. Please look at this answer where it did pass child object list through form parameter. So it can be good starting point.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried it too, still result is same , getting an empty array for events , Like I have posted in question
can you include the JSON you are trying to POST through REST
I am not using rest, I am returning a jsp.. its a simple experiment with mongodb and spring boot
your response might be jsp, but your request 'should' be a JSON, or are you using form to post? basically I want to see what Entity argument is passing into your helloPost method. if it's form parameter, I doubt that your child elements of event are being set.
hmm, Yes I am using form parameter, you are right there must be some issues with my child elements. actually I am new at this, shall i post my jsp form here in edit to my question ?
|

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.