没有从spring控制器和jquery .post()方法中获取正确的json

时间:2022-12-01 15:58:37

I am new at spring framework. I am sending request through the $.post() method from view page and my controller receives data from my DAO class. But I am not getting the full data. I am posting my source code

我是Spring框架的新手。我通过视图页面中的$ .post()方法发送请求,我的控制器从我的DAO类接收数据。但我没有得到完整的数据。我发布了我的源代码

$.post("/diusoftware/showAppraisalFormatWithDetails"+"/41",{id:41},function(data){
            alert(JSON.stringify(data));
        }, "json");

controller

@Controller
public class AppraisalFormatController {
private AppraisalFormatService appraisalFormatService;

@Autowired
public void setAppraisalFormatService(AppraisalFormatService appraisalFormatService) {
    this.appraisalFormatService = appraisalFormatService;
}



//Returning appraisal template list
@RequestMapping(value = "/showAppraisalFormatList", method = RequestMethod.POST)
public @ResponseBody List<AppraisalFormatMaster> showAppraisalFormatList() {

    return appraisalFormatService.getAppraisalFormatList();
}   

// Showing appraisal template create form
@RequestMapping("/createappraisalformat")
public String createAppraisalFormat(Model model) {

    model.addAttribute(new AppraisalFormatMaster());

    return "createappraisalformat";
}


// Save or update data
@RequestMapping(value = "/doCreateAppraisalFormat",method=RequestMethod.POST)
public String doCreateAppraisalFormat(Model model,@RequestBody AppraisalFormatMaster appraisalFormatMaster) {

    appraisalFormatService.create(appraisalFormatMaster); 

    for(AppraisalFormatDetail appraisalFormatDetail:appraisalFormatMaster.getAppraisalFormatDetailList()){
        appraisalFormatDetail.setAppraisalformatmaster(appraisalFormatMaster);
        appraisalFormatService.createAppraisalFormatDetail(appraisalFormatDetail); 
    }       

    model.addAttribute("successMsg", "Data saved successfully !!");
    model.addAttribute("appraisalFormatMaster", new AppraisalFormatMaster());

    return "createappraisalformat";
}



@RequestMapping(path="/showAppraisalFormatWithDetails/{id}", method=RequestMethod.POST, produces="application/json")
public @ResponseBody List<AppraisalFormatMaster> getFormatWithDetails(@PathVariable Integer id,@RequestParam("id") int formatId ){

    System.out.println("showAppraisalFormatWithDetails......................");
    System.out.println("format id: " + formatId);

    for(AppraisalFormatMaster appraisalFormatMaster: appraisalFormatService.getFormatWithDetails(formatId)){

        System.out.println(appraisalFormatMaster);
        for(AppraisalFormatDetail appraisalFormatDetail:appraisalFormatMaster.getAppraisalFormatDetailList()){
            System.out.println(appraisalFormatDetail);
        }
    }


    return appraisalFormatService.getFormatWithDetails(formatId);
}

AppraisalFormatDao Class

@Repository
@Transactional
@Component("appraisalFormatDao")
public class AppraisalFormatDao {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session session(){
        return sessionFactory.getCurrentSession();
    }

    @SuppressWarnings({ "unchecked"})
    public List<AppraisalFormatMaster> getFormatWithDetails(int id){

        DetachedCriteria criteria = DetachedCriteria.forClass(AppraisalFormatMaster.class);     

        criteria.setFetchMode("appraisalFormatDetailList", FetchMode.JOIN);
        criteria.add(Restrictions.eq("id", id));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        return criteria.getExecutableCriteria(session()).list();
    }



    @Transactional
    public void save(AppraisalFormatMaster appraisalFormatMaster) {     
        session().save(appraisalFormatMaster);

    }

    @Transactional
    public void update(AppraisalFormatMaster appraisalFormatMaster) {       
        session().update(appraisalFormatMaster);
    }


    @Transactional
    public void saveAppraisalFormatDetail(AppraisalFormatDetail appraisalFormatDetail) {        
        session().save(appraisalFormatDetail);

    }

    @Transactional
    @SuppressWarnings("unchecked")
    public List<AppraisalFormatMaster> getAppraisalFormatList() {
        DetachedCriteria criteria = DetachedCriteria.forClass(AppraisalFormatMaster.class);

        return criteria.getExecutableCriteria(session()).list();
    }


}

Service Class

@Service
public class AppraisalFormatService {

    @Autowired
    private AppraisalFormatDao appraisalFormatDao;

    public void create(AppraisalFormatMaster appraisalFormatMaster) {

        if(appraisalFormatMaster.getId() > 0){
            appraisalFormatDao.update(appraisalFormatMaster);   
        } else {
            appraisalFormatDao.save(appraisalFormatMaster); 
        }

    }   

    public void createAppraisalFormatDetail(AppraisalFormatDetail appraisalFormatDetail) {
        appraisalFormatDao.saveAppraisalFormatDetail(appraisalFormatDetail);        
    }

    public List<AppraisalFormatMaster> getAppraisalFormatList() {

        return appraisalFormatDao.getAppraisalFormatList();
    }

    public List<AppraisalFormatMaster> getFormatWithDetails(int id){


        return appraisalFormatDao.getFormatWithDetails(id);
    }
}

Master Class

public class AppraisalFormatMaster {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    // @NotNull
    @Column(name = "formatName", unique = true)
    private String formatName;

    @Column(name = "formVersionNo")
    private String formVersionNo;

    @Column(name = "markVersionNo")
    private int markVersionNo;

    @Column(name = "createdBy")
    private Long createdBy;

    @JsonIgnore
    @OneToMany(mappedBy = "appraisalFormatMaster", fetch = FetchType.LAZY)
    private List<AppraisalFormatDetail> appraisalFormatDetailList;



    public AppraisalFormatMaster(){

    }

    public int getId() {
        return id;
    }

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

    public String getFormatName() {
        return formatName;
    }

    public void setFormatName(String formatName) {
        this.formatName = formatName;
    }

    public String getFormVersionNo() {
        return formVersionNo;
    }

    public void setFormVersionNo(String formVersionNo) {
        this.formVersionNo = formVersionNo;
    }

    public int getMarkVersionNo() {
        return markVersionNo;
    }

    public void setMarkVersionNo(int markVersionNo) {
        this.markVersionNo = markVersionNo;
    }

    public Long getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }

    public List<AppraisalFormatDetail> getAppraisalFormatDetailList() {
        return appraisalFormatDetailList;
    }

    public void setAppraisalFormatDetailList(List<AppraisalFormatDetail> appraisalFormatDetailList) {
        this.appraisalFormatDetailList = appraisalFormatDetailList;
    }

    @Override
    public String toString() {
        return "AppraisalFormatMaster [id=" + id + ", formatName=" + formatName + ", formVersionNo=" + formVersionNo
                + ", markVersionNo=" + markVersionNo + ", createdBy=" + createdBy + ", appraisalFormatDetailList="
                + appraisalFormatDetailList + "]";
    }

}

Detail Class

public class AppraisalFormatDetail {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_Appraisalformatmaster")
    private AppraisalFormatMaster appraisalFormatMaster;

    @Column(name = "fk_Appraisalheads")
    //@NotNull
    //@ManyToOne
    //@JoinColumn(name = "fk_Appraisalheads", referencedColumnName="id", foreignKey = @ForeignKey(name = "FK_appraisalformatdetail_headid"))
    private int appraisalHeads;

    //@NotNull
    @Column(name = "appraisor")
    private String appraisor;

    public AppraisalFormatDetail(){

    }

    public AppraisalFormatDetail(AppraisalFormatMaster appraisalformatmaster, int appraisalheads,
            String appraisor) {

        this.appraisalFormatMaster = appraisalformatmaster;
        this.appraisalHeads = appraisalheads;
        this.appraisor = appraisor;
    }

    public AppraisalFormatDetail(int id, AppraisalFormatMaster appraisalformatmaster, int appraisalheads,
            String appraisor) {

        this.id = id;
        this.appraisalFormatMaster = appraisalformatmaster;
        this.appraisalHeads = appraisalheads;
        this.appraisor = appraisor;
    }

    public int getId() {
        return id;
    }

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

    public AppraisalFormatMaster getAppraisalformatmaster() {
        return appraisalFormatMaster;
    }

    public void setAppraisalformatmaster(AppraisalFormatMaster appraisalformatmaster) {
        this.appraisalFormatMaster = appraisalformatmaster;
    }

    public int getAppraisalHeads() {
        return appraisalHeads;
    }

    public void setAppraisalHeads(int appraisalheads) {
        this.appraisalHeads = appraisalheads;
    }

    public String getAppraisor() {
        return appraisor;
    }

    public void setAppraisor(String appraisor) {
        this.appraisor = appraisor;
    }

    @Override
    public String toString() {
        return "AppraisalFormatDetail [id=" + id + ", appraisalHeads=" + appraisalHeads + ", appraisor=" + appraisor + "]";
    }
}

I found output when System.out from controller

我从控制器发现了System.out的输出

AppraisalFormatMaster [id=41, formatName=Template for IT Department, formVersionNo=1, markVersionNo=1, createdBy=0, appraisalFormatDetailList=[AppraisalFormatDetail [id=19, appraisalHeads=1, appraisor=1,2], AppraisalFormatDetail [id=20, appraisalHeads=2, appraisor=1,2], AppraisalFormatDetail [id=21, appraisalHeads=3, appraisor=1,2], AppraisalFormatDetail [id=22, appraisalHeads=4, appraisor=3,4]]]

But I found the output in $.post() method

但我在$ .post()方法中找到了输出

[{"id":41,"formatName":"Template for IT Department","formVersionNo":"1","markVersionNo":1,"createdBy":0}]

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>diusoftware</groupId>
    <artifactId>diusoftware</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.4</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>
    <dependency>
        <groupId>commons-validator</groupId>
        <artifactId>commons-validator</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.7</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>3.0.7</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>3.0.7</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.1-20130918</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.5.4-Final</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.3.0.Final</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.4</version>            
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.4</version>

    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.4</version>

    </dependency>



    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
</dependencies>

Database

CREATE TABLE appraisalformatmaster (
    id INT(11) NOT NULL AUTO_INCREMENT,
    formatName VARCHAR(100) NULL DEFAULT NULL,
    formVersionNo VARCHAR(15) NULL DEFAULT '1',
    markVersionNo INT(11) NULL DEFAULT '1',
    createdBy BIGINT(20) NULL DEFAULT NULL,
    createdTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);


CREATE TABLE appraisalformatdetail (
    id INT(11) NOT NULL AUTO_INCREMENT,
    fk_Appraisalformatmaster INT(11) NOT NULL,
    fk_Appraisalheads INT(11) NULL DEFAULT '1',
    appraisor VARCHAR(15) NULL DEFAULT NULL,
    PRIMARY KEY (id),

);

1 个解决方案

#1


0  

You don't have any of the detail records on the Javascript side because you have @JsonIgnore on the collection inside your AppraisalFormatMaster class.

您在Javascript端没有任何详细记录,因为您在AppraisalFormatMaster类的集合中有@JsonIgnore。

#1


0  

You don't have any of the detail records on the Javascript side because you have @JsonIgnore on the collection inside your AppraisalFormatMaster class.

您在Javascript端没有任何详细记录,因为您在AppraisalFormatMaster类的集合中有@JsonIgnore。