Friday, July 12, 2013

Spring Restful: Get,Add,Update,Delete Restfull WS at Server Side

package com.fpt.family.controler;

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;

import com.fpt.family.util.DateConverter;
import com.jaxb.family.Children;
import com.jaxb.family.Family;
import com.jaxb.family.Father;
import com.jaxb.family.Mother;

@Controller
@RequestMapping(\"fpt/family\")
public class FamilyController {

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/get
    @RequestMapping(value = \"/get\", method = RequestMethod.GET)
    public @ResponseBody
    Family getAllFamily() {

        Children children01 = new Children();
        children01.setFullName(\"Nguyen Trung Hieu Brother\");
        children01.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        children01.setJob(\"Java Engineer\");
        children01.setCompany(\"Fsoft Company\");

        Children children02 = new Children();
        children02.setFullName(\"Nguyen Trung Hieu Brother\");
        children02.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        children02.setJob(\"Java Engineer\");
        children02.setCompany(\"Fsoft Company\");

        Father father = new Father();
        father.setCompany(\"Fsoft Company\");
        father.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        father.setFatherName(\"Nguyen Trung Hieu Father\");
        father.setJob(\"Java Engineer\");

        Mother mother = new Mother();
        mother.setCompany(\"Fsoft Company\");
        mother.setDateOfBirth(DateConverter
                .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                        \"yyyy/MM/dd\"false));
        mother.setMotherName(\"Nguyen Trung Hieu Mother\");
        mother.setJob(\"Java Engineer\");

        Family family = new Family();

        family.setMother(mother);
        family.setFather(father);
        family.getChildren().add(children01);
        family.getChildren().add(children02);

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/get/true
    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/get/false
    /**
     * Get Family
     * @param flag
     * @return
     */

    @RequestMapping(value = \"/get/{flag}\", method = RequestMethod.GET)
    public @ResponseBody
    Family getFamily(@PathVariable(\"flag\") Boolean flag) {
        Family family = null;
        if (flag) {
            Children children01 = new Children();
            children01.setFullName(\"Nguyen Trung Hieu Brother\");
            children01.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children01.setJob(\"Java Engineer\");
            children01.setCompany(\"Fsoft Company\");

            Children children02 = new Children();
            children02.setFullName(\"Nguyen Trung Hieu Brother\");
            children02.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children02.setJob(\"Java Engineer\");
            children02.setCompany(\"Fsoft Company\");

            Father father = new Father();
            father.setCompany(\"Fsoft Company\");
            father.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            father.setFatherName(\"Nguyen Trung Hieu Father\");
            father.setJob(\"Java Engineer\");

            Mother mother = new Mother();
            mother.setCompany(\"Fsoft Company\");
            mother.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            mother.setMotherName(\"Nguyen Trung Hieu Mother\");
            mother.setJob(\"Java Engineer\");

            family = new Family();

            family.setMother(mother);
            family.setFather(father);
            family.getChildren().add(children01);
            family.getChildren().add(children02);
        } else {
            Children children01 = new Children();
            children01.setFullName(\"Nguyen Trung Hieu Brother\");
            children01.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children01.setJob(\"Java Engineer\");
            children01.setCompany(\"Fsoft Company\");

            Children children02 = new Children();
            children02.setFullName(\"Nguyen Trung Hieu Brother\");
            children02.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            children02.setJob(\"Java Engineer\");
            children02.setCompany(\"Fsoft Company\");

            Father father = new Father();
            father.setCompany(\"Fsoft Company\");
            father.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            father.setFatherName(\"Nguyen Trung Hieu Father\");
            father.setJob(\"Java Engineer\");

            Mother mother = new Mother();
            mother.setCompany(\"Fsoft Company\");
            mother.setDateOfBirth(DateConverter
                    .convertStringDateToXmlGregorianCalendar(\"2010/11/20\",
                            \"yyyy/MM/dd\"false));
            mother.setMotherName(\"Nguyen Trung Hieu Mother\");
            mother.setJob(\"Java Engineer\");

            family = new Family();
            family.setMother(mother);
            family.setFather(father);
            family.getChildren().add(children01);
            family.getChildren().add(children02);
        }

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/
    /**
     * For updating family
     * 
     * @param familyRequest
     * @return
     */

    @RequestMapping(value = \"/\", method = RequestMethod.PUT)
    public @ResponseBody
    Family updateFamily(@RequestBody Family familyRequest) {

        Family family = new Family();
        family.setFather(familyRequest.getFather());
        family.setMother(familyRequest.getMother());

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/
    /**
     * For add new family
     * 
     * @param familyRequest
     * @return
     */

    @RequestMapping(value = \"/\", method = RequestMethod.POST)
    public @ResponseBody
    Family addFamily(@RequestBody Family familyRequest) {

        Family family = new Family();
        family.setFather(familyRequest.getFather());
        family.setMother(familyRequest.getMother());

        return family;
    }

    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/delete/true
    // http://localhost:8080/springrestfuljaxb/ws/fpt/family/delete/false
    /**
     * For Delete family: True delete All
     * 
     * @param flag
     * @param familyRequest
     * @return
     */

    @RequestMapping(value = \"/delete/{flag}\", method = RequestMethod.DELETE)
    public @ResponseBody
    Family deleteFamily(@PathVariable(\"flag\") Boolean flag,
            @RequestBody Family familyRequest) {

        Family family = null;

        if (flag) {
            family = new Family();
            family.setFather(familyRequest.getFather());
            family.setMother(familyRequest.getMother());
        } else {
            family = new Family();
            family.setFather(familyRequest.getFather());
            family.setMother(familyRequest.getMother());
        }

        return family;
    }
}
Source Code

No comments:

Post a Comment