通过java webservice发送列表

时间:2015-04-13 02:01:34

标签: java xml web-services list jaxb

我需要创建一个使用xml文件作为其DB的Web服务...即它包含所有细节。我需要获得vin号码或车辆名称或客户端的任何请求并处理它。然后将响应作为相应车辆的列表发送给客户。 所以我创建如下,但是当我尝试将DealerShowRoom对象作为参数传递或将对象返回给客户端时,我得到错误并警告JAX-RPC不支持List类型或ArrayList类型。我该如何向客户发送清单呢??? 我根据名为Processor的架构和服务类创建了jaxb类。它们如下:

VehicleBean.java

package com.server;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VehicleBean {
    private String viNumber;
    private String vehicleName;

    /**
     * @return the viNumber
     */
    public String getViNumber() {
        return viNumber;
    }
    /**
     * @param viNumber the viNumber to set
     */
    public void setViNumber(String viNumber) {
        this.viNumber = viNumber;
    }
    /**
     * @return the vehicleName
     */
    public String getVehicleName() {
        return vehicleName;
    }
    /**
     * @param vehicleName the vehicleName to set
     */
    public void setVehicleName(String vehicleName) {
        this.vehicleName = vehicleName;
    }
}

DealerShowroom.java

package com.server;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DealerShowRoom {
    private String showRoomName;
    private String showRoomLocation;
    private ArrayList<VehicleBean> vehicleList;

    /**
     * @return the showRoomName
     */
    public String getShowRoomName() {
        return showRoomName;
    }
    /**
     * @param showRoomName the showRoomName to set
     */
    public void setShowRoomName(String showRoomName) {
        this.showRoomName = showRoomName;
    }
    /**
     * @return the showRoomLocation
     */
    public String getShowRoomLocation() {
        return showRoomLocation;
    }
    /**
     * @param showRoomLocation the showRoomLocation to set
     */
    public void setShowRoomLocation(String showRoomLocation) {
        this.showRoomLocation = showRoomLocation;
    }
    /**
     * @return the vehicleList
     */
    public ArrayList<VehicleBean> getVehicleList() {
        return vehicleList;
    }
    /**
     * @param vehicleList the vehicleList to set
     */
    public void setVehicleList(ArrayList<VehicleBean> vehicleList) {
        this.vehicleList = vehicleList;
    }
}

Processor.java

package com.server;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Processor {

    //All the necessary class variables that will consumed inside
    JAXBContext context = null;
    ArrayList<VehicleBean> vehicleList = null;
    DealerShowRoom dealerShowRoomObj = null;
    VehicleBean vehicleObj = null;
    File flatFile_XML = null;

    //The destination of the target flat file
    private static final String fileName = "D:\\emulatedDB-jaxb.xml";


    //----------------------------Include Vehicle----------------------------------------------------

    //A Method to include a vehicle into a dealer's list of vehicles
    public void includeVehicle(VehicleBean vehicle){

        //Read the Dealer's object from the XML file
        dealerShowRoomObj = xmlReader();

        //Read the Dealer's list and add the current vehicle to that list
        dealerShowRoomObj.getVehicleList().add(vehicle);

        //Write the list back into the XML file
        xmlWriter(dealerShowRoomObj);
    }

    //-----------------------------Remove Vehicle---------------------------------------------------

    //A method to remove the vehicles from the dealer's list
    public void removeVehicle(DealerShowRoom forRemovalobj){

        //Dealer Object is added as the arguement with the thought that 
        //multiple vehicle deletion option might be needed in the future\
        //So the collective VIN is set in the list of vehicle obj and sent as dealer object

        //XML file is read and a Dealer Object is obtained
        dealerShowRoomObj = xmlReader();

        //The list of vehicles in the Original dealerObject is opened
        ArrayList<VehicleBean> tempVehicleListOriginal = dealerShowRoomObj.getVehicleList();

        //The list of vehicles in the Dealer object sent for removal is opened
        ArrayList<VehicleBean> tempVehicleListProxy = forRemovalobj.getVehicleList();

        //Dual loop running on the two lists
        for(VehicleBean existingVehicle : tempVehicleListOriginal){
            for(VehicleBean vehicleForRemoval : tempVehicleListProxy){
                if(vehicleForRemoval.getViNumber() == existingVehicle.getViNumber()){
                    //Removing the objects from both the list to reduce the number
                    //of loops after every iteration 
                    tempVehicleListOriginal.remove(existingVehicle);
                    tempVehicleListProxy.remove(vehicleForRemoval);
                }
            }
        }
        //setting the updated original list of vehicles in the dealer object
        dealerShowRoomObj.setVehicleList(tempVehicleListOriginal);

        //calling the XML writer to write the updated dealer object 
        //into the XML file
        xmlWriter(dealerShowRoomObj);
    }

    //----------------------------------Update Vehicle-----------------------------------------------

    //A method to update the details of a vehicle in the dealer's list
    public void updateVehicleDetail(VehicleBean vehicle){

        //get the vehicle object from the dealer's list
        vehicleObj = getVehicle(vehicle);

        //set the new data of the vehicle by over writing the existing data
        //Any other variables that exist can be added later
        vehicleObj.setVehicleName(vehicle.getVehicleName());
        vehicleObj.setViNumber(vehicle.getViNumber());

        //Now the change is done in the variables of the object existing in the dealer's list
        //So the dealer object containing the list has to be written again to reflect the change
        // in the XML file
        xmlWriter(dealerShowRoomObj);
    }

    //--------------------------------Fetch Vehicle--------------------------------------------------

    //A method to get the details of the given vehicle
    public VehicleBean getVehicle(VehicleBean vehicle){
        //XML file is read and a Dealer Object is obtained
        dealerShowRoomObj = xmlReader();

        //The list of vehicles in the dealerObject is opened
        ArrayList<VehicleBean> tempVehicleList = dealerShowRoomObj.getVehicleList();

        //A vehicle to hold the resulting vehicle object
        VehicleBean currentVehicle = null;

        //Each vehicle in the list of vehicles is iterated to obtain the one matching
        for (VehicleBean tempVehicle : tempVehicleList) {
            //If the viNumber matches, the vehicle is found
            if(tempVehicle.getViNumber().equals(vehicle.getViNumber())){
                //And it becomes the current vehicle
                currentVehicle = tempVehicle;
                break;
            }
        }
        //return the current Vehicle to the caller
        return currentVehicle;
    }

    //-----------------------------------XML Reader--------------------------------------------------

    //A method to read the list of vehicles into the dealer's object
    private DealerShowRoom xmlReader(){
        //The XML file is read and placed in flatFile_XML
        flatFile_XML = new File(fileName);

        try {
            //If flatFile_XML is not a new File
            if(!flatFile_XML.createNewFile()){          
                context = JAXBContext.newInstance(DealerShowRoom.class);
                Unmarshaller umObj = context.createUnmarshaller();
                dealerShowRoomObj = (DealerShowRoom) umObj.unmarshal(flatFile_XML);
            }else{
                System.out.println("Sorry! The file is empty!!!");
            }
        } catch(JAXBException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return dealerShowRoomObj;
    }

    //------------------------------------XML Writer-------------------------------------------------

    //A method to write the dealer's list of vehicles into the XML file from the dealer's object
    private void xmlWriter(DealerShowRoom dealerShowRoomObj){
        //A method just to write the array list into the XML file
        try {
            context = JAXBContext.newInstance(DealerShowRoom.class);
            Marshaller marshObj = context.createMarshaller();

            //This is allign the XML file properly
            marshObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            // Write to System.out
            marshObj.marshal(dealerShowRoomObj, System.out);

            // Write to File
            marshObj.marshal(dealerShowRoomObj, flatFile_XML);
        } 
        catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案