Ajax request responding with status 500

Asked

Viewed 104 times

1

I am using Spring, I test the return in the service and controller methods and are correctly returning what I want, and is entered in the database, but even so gives the error:

inserir a descrição da imagem aqui

My request:

$("#formNew").validate({

        submitHandler: function(form) {
            var tbl = $('#list').DataTable();
            var value = $('#plc_db-new').val();
            var sensors = $('#sensors-new').val();

            $('#btn-new').prop('disabled', true);
            $('#formNew .ico-loading-save').removeClass('hidden');
            $.ajax({
                url: Config.path+"/admin/plc_db/?plcId="+plcId,
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data: JSON.stringify({value:value, sensorsIds:sensors}),
                success : function(data, success, response){
                    switch(response.status){
                        case 200:
                            tbl.row.add({
                                "id": data.id,
                                "value": data.value,
                            }).draw();
                            clearNew();
                            clearMessages();
                            $('#modalNew').modal('hide');
                            $('#btn-new').prop('disabled', false);
                            $('#formNew .ico-loading-save').addClass('hidden');
                            $('#message .text').text('Plc_Db inserido com sucesso!');
                            $('#message').addClass('alert-success').removeClass('hidden');
                    }
                }
            });

            return false;
        }
    });

Controller:

@RequestMapping(value="/", method=RequestMethod.POST, produces="application/json")
public @ResponseBody Plc_db create(@RequestBody Plc_db entity, @RequestParam(value="plcId") Integer plcId) {
    return service.createOrUpdatePlcDb(entity, plcId);
}

Service:

public Plc_db createOrUpdatePlcDb(Plc_db entity, Integer plcId) {
    entity.setPlc_config(plcRepository.getOne(plcId));
    Plc_db plc_db = repository.save(entity);
    if (entity.getSensorsIds() != null) {
        for(Integer idSensor: entity.getSensorsIds()){
            Sensor sensor = sensorRepository.getOne(idSensor);
            sensor.setDb(plc_db);
            sensorRepository.save(sensor);
        }
    }
    return plc_db;
}

Entity:

package io.ubivis.admin.plc_db.model;


import com.fasterxml.jackson.annotation.JsonIgnore;
import io.ubivis.admin.core.sensor.model.Sensor;
import io.ubivis.admin.plc.model.Plc;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Entity
public class Plc_db implements Serializable {
    private Integer id;
    private String value;
    private Plc plc_config;
    @JsonIgnore
    private List<Sensor> sensors;
    private Integer[] sensorsIds;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Transient
    public Integer[] getSensorsIds() {
        return sensorsIds;
    }

    public void setSensorsIds(Integer[] sensorsIds) {
        this.sensorsIds = sensorsIds;
    }

    @ManyToOne
    public Plc getPlc_config() {
        return plc_config;
    }

    public void setPlc_config(Plc plc_config) {
        this.plc_config = plc_config;
    }

    @OneToMany(mappedBy = "db")
    public List<Sensor> getSensors() {
        return sensors;
    }

    public void setSensors(List<Sensor> sensors) {
        this.sensors = sensors;
    }
}

Edit

Warn do Spring:

2019-05-03 10:20:42.386 WARN 6672 --- [ qtp9203204-45] .w.s.m.s.Defaulthandlerexceptionresolver : Failed to write HTTP message: org.springframework.http.converter.Httpmessagenotwritableexception: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: io.ubivis.admin.plc_db.model.Plc_db["plc_config"]->io.ubivis.admin.plc.model.Plc_$$_jvstd9_c["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: io.ubivis.admin.plc_db.model.Plc_db["plc_config"]->io.ubivis.admin.plc.model.Plc_$$_jvstd9_c["handler"])

  • 1

    Put the spring stack trace, error 500 is that something went wrong on the server

  • I put it there on DIT

  • Httpmessagenotwritableexception -> impossible to write to the server which may explain the 500 error. Change the CHMOD of the folders to view.

1 answer

-1


John I already went through this a while ago, you can add to Application Properties File

spring.jackson.serialization.fail-on-empty-beans=false

or annotate its entity with

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.