Reputation: 5933
I just started using the Rest Implementation of the Spring with Mongodb as a Backend and UI as Backbone Client.
I have done the configuration part of Spring with Mongo Db as well as creating the Client with Backbone.
I got stuck in PUT request and POST request.
@Document(collection = "contacts")
public class ContactModel {
@Id
private String id;
private String name;
private String phone;
//getter and setters
//Overriden toString()
}
@Controller
@RequestMapping (value="contacts")
public class ContactController implements ApplicationContextAware{
private ApplicationContext applicationContext;
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody List<ContactModel> getContacts(){
//contacts is a list
return contacts;
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody ContactModel getContact(@PathVariable String id){
//contact is a single contact
return contact;
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public @ResponseBody ContactModel updateContact(@PathVariable String id,@RequestBody ContactModel contactModel){
System.out.println("I am put " + id);
System.out.println("I am put 2 " + contactModel);
return null;
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public @ResponseBody ContactModel deleteContact(@PathVariable String id){
//deleting the Document with id as *id* and returning it
return result;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}
Its just a basic controller, not a large computation is going on. Now, I am able to hit the GET (both with contacts/ and contact/{id}) and DELETE.
Also I am able to hit the PUT also from the Backbone view, the Request payload that is going is :
{id: "5285fcb9ae67a6209c5b3567", name: "ABC DEF", phone: "2342465678"}
Also in my PUT method I am getting the :
I am put 5285fcb9ae67a6209c5b3567
I am put 2 ContactModel [id=5285fcb9ae67a6209c5b3567, name=null, phone=null]
Now the problem is why the value of name and phone is null and that of id is set, if Spring is unable to push into the model why id is getting set in the variable, contactModel of the function. Its really painful :'(
Request URL:http://localhost:8080/karma/contacts/5285fcb9ae67a6209c5b3567
Request Method:PUT
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6,nb;q=0.4
Connection:keep-alive
Content-Length:69
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/karma/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
X-Requested-With:XMLHttpRequest
Please shed some thoughts, thanks in advance.
Upvotes: 0
Views: 138
Reputation: 5933
Ok it started working, the problem was the old class files, although the Eclipse is recompiling the files but the class files are not getting reflected, in the Tomcat,
Now the mapping of the object started..this is really unbelievable...
:D
Anyways thanks for the replies
Upvotes: 2
Reputation: 81
Try to check whether content type header application/json was set when sending the request
Upvotes: 1