类具有名称相同的两个属性“ address1”

时间:2019-06-23 11:57:27

标签: java jaxb

我想使用以下代码创建JAXB对象:

if (!function_exists('imageOrientation'))
{
function imageOrientation(string $directory)
{
    if(file_exists($directory))
    {
        $destination_extension = strtolower(pathinfo($directory, PATHINFO_EXTENSION));
        if(in_array($destination_extension, ["jpg","jpeg"]) && exif_imagetype($directory) === IMAGETYPE_JPEG)
        {
            if(function_exists('exif_read_data'))
            {
                $exif = exif_read_data($directory);
                if(!empty($exif) && isset($exif['Orientation']))
                {
                    $orientation = $exif['Orientation'];
                    switch ($orientation)
                    {
                        case 2:
                            $flip = 1;
                            $deg = 0;
                            break;
                        case 3:
                            $flip = 0;
                            $deg = 180;
                            break;
                        case 4:
                            $flip = 2;
                            $deg = 0;
                            break;
                        case 5:
                            $flip = 2;
                            $deg = -90;
                            break;
                        case 6:
                            $flip = 0;
                            $deg = -90;
                            break;
                        case 7:
                            $flip = 1;
                            $deg = -90;
                            break;
                        case 8:
                            $flip = 0;
                            $deg = 90;
                            break;
                        default:
                            $flip = 0;
                            $deg = 0;
                    }
                    $img = imagecreatefromjpeg($directory);
                    if($deg !== 1 && $img !== null)
                    {
                        if($flip !== 0)
                        {
                            imageflip($img,$flip);
                        }
                        $img = imagerotate($img, $deg, 0);
                        imagejpeg($img, $directory);
                    }
                }
            }
        }
    }
}
}

内部对象:

@XmlRootElement(name = "payment_transaction")
@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorizeRequest {

    @XmlElement(name = "transaction_type")
    public String transaction_type;

    @XmlElement(name = "transaction_id")
    public String transaction_id;

    .......

    public String getTransaction_type() {
        return transaction_type;
    }

    public void setTransaction_type(String transaction_type) {
        this.transaction_type = transaction_type;
    }

    public String getTransaction_id() {
        return transaction_id;
    }       
}

但是我得到了错误:

public class Address {

    @XmlElement(name = "first_name")
    public String firstName;

    @XmlElement(name = "last_name")
    public String lastName;

    ... getters and setters
}

您知道如何解决此问题吗?我需要为Address类添加一些注释吗?

1 个答案:

答案 0 :(得分:2)

尝试使用未指定@XmlAccessorType参数的类的实例添加字段时,您可能会遇到类似的错误。

例如,您有:

@XmlRootElement(name = "payment_transaction")
@XmlAccessorType(XmlAccessType.FIELD)
public class AuthorizeRequest {
    ...
    private Data data;

    // getters/setters
}

Data类结构如下:

public class Data {
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

但是,由于您没有指定用于访问@XmlAccessorType类字段的Data参数,因此默认情况下将同时通过字段和方法执行搜索。因此,地址字段与getAddress方法冲突。