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;
}
}
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? –
Proszę pana, nie ma pola 'valid' w moim' BinderResponse'. Mam tylko szczegóły adresu i jest to 'getter/setter'. –
@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 ??? –