1

I have a method, which changes the status from 'Active' to 'InActive' and vice

versa, of a record, by fetching it's id.

Now, I want to convert it to return a ResponseEntity object, inside which, I have a map stored

When I test my method, I get a 400: Bad Request

////////////////////////Old Method////////////////

@RequestMapping("toggleStatus")
    public @ResponseBody void toggleStatus(@RequestParam("resourceId") Long resourceId ){
        ResourceElementMaster resourceElementMaster = resourceElementService.findById(resourceId);
        if(resourceElementMaster.getIsActive() == true) {
            resourceElementMaster.setIsActive(false);
        } else {
            resourceElementMaster.setIsActive(true);
        }
        resourceElementService.update(resourceElementMaster);
    }

//////////////////////New Method/////////////////////////////

@RequestMapping(value="toggleStatus",method=RequestMethod.PUT)
    public @ResponseBody ResponseEntity<Map<String, Object>> toggleStatus(@RequestBody Long resourceId ){
        Map<String, Object> mapToggle=new HashMap<String, Object>();
        ResourceElementMaster resourceElementMaster = resourceElementService.findById(resourceId);
        if(resourceElementMaster.getIsActive() == true) {
            resourceElementMaster.setIsActive(false);
        } else {
            resourceElementMaster.setIsActive(true);
        }
        mapToggle.put("Update",resourceElementService.update(resourceElementMaster));
        return new ResponseEntity<Map<String, Object>>(mapToggle, HttpStatus.OK) ;
    }

How do I solve this??

1
  • The JSON which I pass, to test this method, is : { "resourceId": 57, "isActive":true } Commented Nov 16, 2016 at 10:18

2 Answers 2

1

You problem is, you are trying to pass an json to your /toggleStatus Method. But your @Controller accepts only a resourceIdof type Long.

So your response via Postman should be something like this:

PUT http://localhost:8080/toggleStatus?resouceId=42

also, there is no need for RequestEntity in your case. Modify your @controller like this:

@ResponseBody 
@RequestMapping(value = "toggleStatus", method = RequestMethod.PUT)
public Object toggleStatus(@RequestParam Long resourceId ){
    Map<String, Object> mapToggle = new HashMap<String, Object>();
    ResourceElementMaster resourceElementMaster = resourceElementService.findById(resourceId);
    resourceElementMaster.setIsActive(!resourceElementMaster.getIsActive());
    mapToggle.put("Update", resourceElementService.update(resourceElementMaster));
    return mapToggle;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Same : 400 Bad Request.
How do I test it?? I passed the following JSON in Postman : { "resourceId": 57, "version":1, "name":"Allegation1", "Active":true }
Why you are passing a json? Your Controller accepts only a resourceId.
0

Don't pass a json, you only need to pass the resourceId, which you are accepting as a parameter in your method toggleStatus.

Also, use @RequestParam, instead of @RequestBody :

@RequestMapping(value="toggleStatus",method=RequestMethod.POST)
public @ResponseBody ResponseEntity<Map<String, Object>> toggleStatus(@RequestParam(value = "resourceId") Long resourceId ){
    Map<String, Object> mapToggle=new HashMap<String, Object>();
    ResourceElementMaster resourceElementMaster = resourceElementService.findById(resourceId);
    if(resourceElementMaster.getIsActive() == true) {
        resourceElementMaster.setIsActive(false);
    } else {
        resourceElementMaster.setIsActive(true);
    }
    mapToggle.put("Update",resourceElementService.update(resourceElementMaster));
    return new ResponseEntity<Map<String, Object>>(mapToggle, HttpStatus.OK) ;
}

Comments

Your Answer

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