2017-04-23 2 views
-1
@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; 
    } 

Dieser Code funktioniert ordnungsgemäß. aber wenn eine Änderung der 3. und her Linien des öffentlichenrestful- java private vs public

public String description; 
public int duration; 

ich Fehler bekommen 500. Ich verstehe nicht, was mit public Elementtyp falsch ist. Jedes Objekt kann auf den Membertyp public zugreifen. Bitte erläutern Sie auch, wann Sie public oder private verwenden müssen?

+1

Was ist der Sinn von Getter- und Einstellungsmethoden, wenn Sie das Feld veröffentlichen? Wenn Sie es öffentlich machen, sieht das System zwei Werte mit demselben Namen, das Feld und die Eigenschaft (get/set). Entfernen Sie die Get/Set-Methoden, oder behalten Sie das Feld privat. – Andreas

Antwort

1

Fehler 500 ist nicht der wahre Fehler. Schau dir deine Logs an und du wirst wahrscheinlich den echten Fehler sehen. Hier

ist ein MCVE (Minimal, Complete, und prüfbare Beispiel):

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; 
    } 
} 

Lauf es diesen Fehler geben:

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) 

Wie Sie sehen können, so dass das Feld public bedeutet, dass JAXB sieht zwei gleichnamige Eigenschaften, eine durch das public definierte Feld, und eine durch die get/set definierte Bohnenmethode.

Da es keinen Sinn macht, Getter- und Setter-Methoden zu verwenden, wenn Sie das Feld public machen, entfernen Sie die Methoden, wenn Sie möchten, dass die Felder public sind.

Ich würde vorschlagen, sie nicht public zu machen. Warum würdest du? Es gibt keine Notwendigkeit, wenn Sie die Getter- und Setter-Methoden für den Zugriff auf das Feld haben.