Announcement Announcement Module
Collapse
No announcement yet.
RESTful nested data CRUD Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RESTful nested data CRUD

    Code:
    package it.celi.web;
    
    import flexjson.JSONDeserializer;
    import flexjson.JSONSerializer;
    import it.celi.AclService;
    import it.celi.security.BusinessFunction;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    import lombok.Setter;
    
    import org.springframework.beans.BeanUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class BusinessFunctionsController {
    	
    	@Setter
    	@Autowired
    	private AclService aclService;
    	
    	/* READ */
    	@RequestMapping(value="/businessFunctions", method=RequestMethod.GET)
    	@ResponseBody
    	public String getAll() {
    		return serializeJson(aclService.getBusinessFunctions());
    	}
    	
    	@RequestMapping(value="/businessFunctions/{id}", method=RequestMethod.GET)
    	@ResponseBody
    	public String get(@PathVariable String id) {
    		Collection<BusinessFunction> businessFunctions = new ArrayList<BusinessFunction>();
    		BusinessFunction bfFound = aclService.getBusinessFunctionById(id);
    		businessFunctions.add(bfFound);
    		return serializeJson(businessFunctions);
    	}
    	
    	/* CREATE */
    	@RequestMapping(value="/businessFunctions", method=RequestMethod.POST)
    	public HttpResponse post(@RequestBody String body) {
    		BusinessFunction bfToInsert = deserializeJson(body);
    		aclService.insertBusinessFunction(bfToInsert);
    		return new HttpResponse(HttpResponse.HTTP_CREATED);
    	}
    	
    	/* UPDATE */
    	@RequestMapping(value="/businessFunctions/{id}", method=RequestMethod.PUT)
    	public HttpResponse put(@RequestBody String body, @PathVariable String id) {
    		BusinessFunction bfToInsert = deserializeJson(body);
    		BusinessFunction bfFromDb = aclService.getBusinessFunctionById(id);
    		BeanUtils.copyProperties(bfToInsert, bfFromDb);
    		aclService.updateBusinessFunction(bfFromDb);
    		return new HttpResponse(HttpResponse.HTTP_SUCCESS);
    	}
    
    	/* DELETE */
    	@RequestMapping(value="/businessFunctions/{id}", method=RequestMethod.DELETE)
    	public HttpResponse delete(@PathVariable String id) {
    		aclService.deleteBusinessFunction(id);
    		return new HttpResponse(HttpResponse.HTTP_SUCCESS);
    	}
    	
    	private String serializeJson(Collection<BusinessFunction> businessFunctionList) {
    		JSONSerializer serializer = new JSONSerializer();
    		return serializer.exclude("*.class").serialize(businessFunctionList);
    	}
    	
    	private BusinessFunction deserializeJson(String jsonInput) {
    		JSONDeserializer<BusinessFunction> deserializer = new JSONDeserializer<BusinessFunction>();
    		return deserializer.use(null, BusinessFunction.class).deserialize(jsonInput);
    	}
    }
    Here's my RESTful CRUD web service.
    BusinessFunction is the simplest object on earth:
    two string fields bean, nothing more.

    Now I want to make another service for 'Role' object. Role has got two string fields, too. In addition to that, it holds a collection (or list) of BusinessFunctions.

    My question is: how can I implement such a PUT method? how the json object will appear like? Finally, how can I manage this 'nested' data? can you give me some advice / suggestions please?

    Thanks a lot for your time.
    F.B.
Working...
X