restful- java private vs public

时间:2017-04-23 05:28:16

标签: java rest

@XmlRootElement
public class Activity {
    private String description;
    private int duration;
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getDuration() {
        return duration;
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }

这是代码正常工作。但是当第3和第4行改为公开时​​

public String description;
public int duration;

我收到错误500.我不明白public成员类型有什么问题。每个对象都可以访问public成员类型。另请说明何时使用publicprivate

1 个答案:

答案 0 :(得分:1)

错误500不是真正的错误。查看您的日志,您可能会看到真正的错误。

这是MCVE(最小,完整和可验证的示例):

import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlRootElement;

public class Test {
    public static void main(String[] args) throws Exception {
        JAXBContext.newInstance(Activity.class);
    }
}
@XmlRootElement
class Activity {
    public String description;
    public int duration;
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getDuration() {
        return duration;
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }
}

运行它会出现此错误:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "description"
    this problem is related to the following location:
        at public java.lang.String Activity.getDescription()
        at Activity
    this problem is related to the following location:
        at public java.lang.String Activity.description
        at Activity
Class has two properties of the same name "duration"
    this problem is related to the following location:
        at public int Activity.getDuration()
        at Activity
    this problem is related to the following location:
        at public int Activity.duration
        at Activity

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at Test.main(Test.java:7)

如您所见,创建字段public意味着JAXB会看到两个同名的属性,一个由public字段定义,另一个由get / {定义{1}} bean方法。

如果您创建字段set,则使用getter和setter方法没有任何意义,如果您希望字段为public,请删除方法。

我建议不要制作public。你为什么这样?当你有getter和setter方法访问该字段时,没有必要。