有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将包含对象数组的JSON对象发送到spring控制器?

我使用Hibernate@OneToMany映射了两个域模型。我试图在前端创建一个JSON对象,并将其发送给SpringMVC控制器,以自行设置模型数据

以下是我的模型类: 概念模型细节。爪哇

@Entity
@Table(name="conceptModelDetails")
@SequenceGenerator(name="CONCEPT_SEQ",sequenceName="concept_sequence", initialValue=1, allocationSize=1)
public class ConceptModelDetails implements java.io.Serializable{
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CONCEPT_SEQ")
    private int instructionsId;
    private String operationType;
    private String conceptModelID;
    private String requestor;
    private String status;
    private Timestamp requestDateTime;
    private Timestamp lastExecutedDateTime;
    private Timestamp completedDateTime;
    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="conceptModelDetails")
    @JsonManagedReference   // nested exception is org.springframework.http.converter.HttpMessageNotWritableException: 
    //Could not write JSON: Infinite recursion
//The fix is to get Jackson to be able to handle bi-directional references
    private List<Instructions> instructions = new ArrayList<Instructions>(); 




    public ConceptModelDetails() {
        // TODO Auto-generated constructor stub
    }


//setter & getter methods        
}

和说明。爪哇:

@Entity
@Table(name="instructions")
@SequenceGenerator(name="INSTRUCTIONS_SEQ", sequenceName="instructions_sequence",initialValue=1, allocationSize=1)
public class Instructions implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="INSTRUCTIONS_SEQ")
    private int Sno;
    private String instruction;
    @ManyToOne
    @JoinColumn(name="instructionsId")
    @JsonBackReference
    private ConceptModelDetails conceptModelDetails;


    //setter & getter methods
}

这是我在前端创建和发送JSON对象的send方法:

$scope.send = function() {
    console.log("test");
    var dataObj = {
        "operationType" : $scope.operationType,
        "conceptModelID" : $scope.conceptID,
        "requestor" : $scope.requestor,
        "status" : "new",
        "requestDateTime" : null,
        "lastExecutedDateTime" : null,
        "completedDateTime" : null,
        "instructions" : null

    };
    console.log(dataObj);
    dataObj.instructions = [];    
    console.log($scope.operations_publish);
    var ins = getSelected();
    for ( var i in ins) {
        var temp = {

            instruction : null,
            conceptModelDetails : null

        }
        temp.instruction = ins[i];
        dataObj.instructions.push(temp);
    }
    var response = $http.post(
            'PostService', dataObj);
    response.success(function(data, status, headers, config) {
        $scope.responseData = data;
    });
    response.error(function(data, status, headers, config) {
        alert("Exception details: " + JSON.stringify({
            data : data
        }));
    });
}

以下是我的控制器:

@RequestMapping(value = "/PostService", method = RequestMethod.POST)
public @ResponseBody String Test(@RequestBody ConceptModelDetails conceptModelDetails){
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    ConceptModelDAO obj = (ConceptModelDAO) context.getBean("objDAO");
    System.out.println("concept id: "+conceptModelDetails.getConceptModelID()+" "+ conceptModelDetails.getInstructionsId());
    System.out.println("instructions id: "+conceptModelDetails.getInstructions());
    // ConceptModelDAOImpl objDAO = new ConceptModelDAOImpl();
    obj.add(conceptModelDetails);
    Instructions instructions = new Instructions();
    System.out.println("dimba: " + instructions.getInstruction());
    ArrayList<Instructions> operations = (ArrayList<Instructions>) conceptModelDetails.getInstructions();
    for (int i = 0; i< operations.size(); i++ ) {
        instructions.setInstruction(operations.get(i).getInstruction());
        instructions.setConceptModelDetails(conceptModelDetails);
        obj.addInstructions(instructions);
    }
    return null;
}

由于List<Instructions> instructions,我得到了eror:400(错误请求)。请建议我如何处理这个问题


共 (1) 个答案

  1. # 1 楼答案

    我在这段代码中发现了问题。正如波佐所解释的那样,
    ArrayList<Instructions> operations = (ArrayList<Instructions>) conceptModelDetails.getInstructions();

    应该是

    List<Instructions> operations = conceptModelDetails.getInstructions(); 在弹簧控制器中