2014-04-04 8 views
8

Próbuję wyświetlić dane DB na mojej stronie internetowej. Zrobiłem następujący kod, gdy żądanie GET do @RequestMapping(value = "/api/binder").Uzyskiwanie wyjątku JsonMappingException podczas wysyłania danych do wyświetlenia

ale gdy żądanie get przyszedł do tej metody będzie pobierał dane (Mam drukować na konsoli i wyświetla dobrze), ale to nie ma map na moje wezwanie Java Script Ajax, to pokazuje mi błąd.

Poniżej mój kod, aby pobrać dane:

@Autowired 
    IBinderViewRepository repository; 

    @RequestMapping(method= RequestMethod.GET) 
    public @ResponseBody 
    List<BinderResponse> getBinders(){ 
     List<BinderView> binders = repository.getBinders(); 
     List<BinderResponse> responses = new ArrayList<>(); 
     ModelMapper mapper = Mapper.getInstance(); 

     for(int i = 0; i < binders.size(); i++){ 
      System.out.println("In Loop"); 
      BinderResponse response = mapper.map(binders.get(i),BinderResponse.class); 
      System.out.println("Data :: " + response.getBinderName()); 
      responses.add(response); 
     } 
     return responses; 
    } 

ale pokazuje mi następujący błąd:

HTTP Status 500 - Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]) 

Oto ajax połączeń z nokautem JS:

ajax.get('api/binder').done(function(response){ ... }

Tutaj BinderView and BinderResponse mają te same pola:

private String binderName; 
    private String binderAddress1; 

i setera gettera również w obu. i repository.genBinders() metoda przynoszą dane z DB.

Oto metoda insert i działa dobrze dla mnie:

@RequestMapping(method= RequestMethod.POST,consumes = "application/json") 
    public @ResponseBody 
    IWebApiResponse addBinder(@RequestBody AddBinderForm binder){ 
     ..... 
    } 

będę musiał umieścić dowolny json annotation on my BinderResponse class ?

Nie rozumiem, gdzie się mylę Każdy Wnioskuje prowadź mnie?.

UPDATE:

public class BinderResponse extends WebApiResponseBase { 
    private String binderName; 
    private String binderAddress1; 

public String getBinderName() { 
     return binderName; 
    } 

    public void setBinderName(String binderName) { 
     this.binderName = binderName; 
    } 

    public String getBinderAddress1() { 
     return binderAddress1; 
    } 

    public void setBinderAddress1(String binderAddress1) { 
     this.binderAddress1 = binderAddress1; 
    } 
} 

BinderView:

public class BinderView extends BaseView { 
     private String binderName; 
     private String binderAddress1; 
    public String getBinderName() { 
      return binderName; 
     } 

     public void setBinderName(String binderName) { 
      this.binderName = binderName; 
     } 

     public String getBinderAddress1() { 
      return binderAddress1; 
     } 

     public void setBinderAddress1(String binderAddress1) { 
      this.binderAddress1 = binderAddress1; 
     } 

} 

W konsoli drukuje dane/BinderName:

In Loop 
Data :: ada 
In Loop 
Data :: tya 

New Update:

Oto BaseView:

@MappedSuperclass 
public abstract class BaseView implements IEntity { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue 
    @Column(name="id") 
    private long id; 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     if (this.id != 0 && this.id != id) { 
      throw new IllegalStateException(
        "The ID must not be changed after it is set."); 
     } 
     this.id = id; 
    } 
} 

i In IEntity:

public interface IEntity extends Serializable { 
    long getId(); 
    void setId(long id); 
} 

WebApiResponseBase:

public class WebApiResponseBase implements IWebApiResponse { 

    private String _uri; 

    @Override 
    public String getUri() { 
     return _uri == null ? "" : _uri; 
    } 

    @Override 
    public void setUri(String uri) { 
     _uri = uri; 
    } 
} 
+0

Zobacz, co zawiera twoje "odpowiedzi". Wygląda na to, że masz pole 'BinderResponse' o nazwie' valid', które ma wartość 'null'. Co to za pole? –

+0

Proszę pana, nie ma pola 'valid' w moim' BinderResponse'. Mam tylko szczegóły adresu i jest to 'getter/setter'. –

+0

@SotiriosDelimanolis: sir kiedy zwracam 'NULL' z metody to nie pokazuje żadnego błędu, myślę, że jest problem z parsowaniem, czy też? powiesz mi czy mam rację czy ??? –

Odpowiedz

23

Jackson domyślnie serializuje hierarchię dziedziczenia całego obiektu, tj. także pola klasy nadrzędnej.W przypadku

public class BinderResponse extends WebApiResponseBase { 

wydaje się

Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->com.ngl.dto.outgoing.BinderResponse["valid"]) 

Jackson próbuje serializacji pole o nazwie valid z getter zwanego isValid (który jest konwencjonalnym fasoli nazwa właściwości). Metoda getter wydaje się jednak z dowolnego powodu rzucać NullPointerException.

Jeśli chcesz, aby Jackson to zignorował, możesz dodać adnotację do pobierającego z @JsonIgnore lub swoją klasą przy pomocy @JsonIgnoreProperties i podać nazwę właściwości, np. valid.

+0

Sir teraz dostaniesz jeden problem, że kiedy jestem zalogowany proces trwa, w klasie kontrolera logowania zwrócił wiadomość do użytkownika z powodu @JsonIgnore tak się dzieje. Więc jeśli będę logował się przy użyciu błędnych danych, to nie wyświetli mi żadnego komunikatu o błędzie. –

-1
@Column(name="createddate") 
private Date createdDate; 

@Transient 
private String formatedCreatedDate; 


public String getFormatedCreatedDate() { 
    DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy"); 
    return dateFormat.format(this.getCreatedDate()); 
} 

To rzuca ten sam wyjątek, bo tu może być zerowy poprzez wywołanie wartość getCreatedDate() pochodzą więc nie można formatować zerową datę więc utrzymać zerową sprawdzanie jak:

Rozwiązanie

public String getFormatedCreatedDate() { 
    DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy"); 
    Date createDdate=this.getCreatedDate(); 
    **if(createDdate!=null){ 
     return dateFormat.format(createDdate); 
    }** 
    return "-"; 
} 
Powiązane problemy