简体   繁体   English

Spring Boot REST API 给出空行

[英]Spring Boot REST API gives empty rows

I am new to Spring boot.我是 Spring Boot 的新手。 I have a MYSQL table "customer" with data as shown: Data in the table When testing the API output using Postman, there seems to be rows of empty JSON output.我有一个带有数据的 MYSQL 表“客户”,如下所示:表中的数据使用 Postman 测试 API 输出时,似乎有几行空的 JSON 输出。

API Output API 输出

Below is my code:下面是我的代码:

package com.semika.customer;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer") 
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

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

    public Customer() {
       super();

    }

}

CustomerRepository客户资料库

package com.semika.customer;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long>{

}

CustomerService客户服务

package com.semika.customer;

public interface CustomerService {
    public Iterable<Customer> findAll(); 
}

CustomerServiceImpl客户服务接口

package com.semika.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService{

    @Autowired
    private CustomerRepository customerRepository;

    public Iterable<Customer> findAll() {
        return customerRepository.findAll(); 
    }
}

CustomerController客户控制器

package com.semika.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService  customerService;

    @RequestMapping("/customers") 
    @ResponseBody
    public Iterable<Customer> findAll() {
       Iterable<Customer> customers = customerService.findAll();
       return customers;
    }
}

I don't know what else I need to modify in the controller to be able to see the output with data.我不知道我还需要在控制器中修改什么才能看到带有数据的输出。

At first look your code seems fine.乍一看,您的代码看起来不错。 So, I copied your code and try to run it and got an empty response just like you.所以,我复制了你的代码并尝试运行它,但得到了一个和你一样的空响应。 After spending some time I figured out the reason why.花了一些时间后,我找到了原因。

Use getter and setter in you customer class and recompile the code.

It will solve your problem.它会解决你的问题。 Also do the following changes:还要进行以下更改:

1) Annotate CustomerRepository with @Repository
2) use @EnableJpaRepositories("package path") in your application's main class if your repository is not in the same or sub package.
3) use method type or @GetMapping annotation in your controller.

For your convenience I am writing your code after all the modifications:为了您的方便,我在所有修改后编写您的代码:

TestDemoApplication.java测试演示应用程序

    package testdemo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

    @SpringBootApplication
    @EnableJpaRepositories("put repository path here")
    public class TestDemoApplication {

        public static void main(String[] args) {
            SpringApplication.run(TestDemoApplication.class, args);
        }
    }

CustomerServiceImpl.java CustomerServiceImpl.java

package testdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService{

    @Autowired
    private CustomerRepository customerRepository;

    public Iterable<Customer> findAll() {
        return customerRepository.findAll(); 
    }
}

CustomerService.java客户服务.java

package testdemo;

public interface CustomerService {
    public Iterable<Customer> findAll(); 
}

CustomerRepository.java客户资源库

package testdemo;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>{

}

CustomerController.java客户控制器.java

package testdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {

    @Autowired
    private CustomerService  customerService;

    @GetMapping(value = "/customers") 
     public Iterable<Customer> findAll() {
       Iterable<Customer> customers = customerService.findAll();
       return customers;
    }
}

Customer.java客户.java

package testdemo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer") 
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

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

    public Customer() {
       super();

    }

    public Long getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

Also, CrudRepository returns a Iterable<> in findAll().此外,CrudRepository 在 findAll() 中返回一个 Iterable<>。 It is the JpaRepository which returns List<> so, no worries about it.返回 List<> 的是 JpaRepository 所以不用担心。

You have used Iterable<Customer> . 您已使用Iterable<Customer> Please use List<Customer> instead of Iterable<Customer> everywhere. 请在各处使用List<Customer>而不是Iterable<Customer>

You may need to iterate over the dataset and add the results to a List or as Vishal stated, change your interfaces and implementations to return a List rather than an Iterable .您可能需要遍历数据集并将结果添加到List或如 Vishal 所述,更改您的接口和实现以返回List而不是Iterable

package com.semika.customer;

import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService  customerService;

    @RequestMapping("/customers") 
    @ResponseBody
    public Iterable<Customer> findAll() {
        List<Customer> results = new ArrayList<>();
        Iterator<Customer> iter = customerService.findAll().iterator();
        while (iter.hasNext()) results.add(iter.next());
        return results;
    }
}

In the following post , Andy states,在下面的帖子中安迪说,

While a List is guaranteed to be an Iterable an Iterable may not be a List .虽然List保证是Iterable ,但Iterable可能不是List This means that if you do cast an Iterable to a List it may fail at runtime.这意味着如果您将Iterable转换为List它可能会在运行时失败。 Even if it works, there's no guarantee that it will continue to work in the future as it could change in new versions of Spring Data JPA without breaking the interface's contract.即使它有效,也不能保证它将来会继续工作,因为它可能会在新版本的 Spring Data JPA 中发生变化而不会破坏接口的契约。

Instead of using a cast, you should declare your own query methods that return List .您应该声明自己的返回List的查询方法,而不是使用List

Also noted in that post, you can use JpaRepository rather than CrudRepository because JPA will return a List rather than an Iterable as mentioned here .JpaRepository文章中还指出,您可以使用JpaRepository而不是CrudRepository因为 JPA 将返回一个List而不是这里提到的Iterable

I am new to Spring boot.我是Spring Boot的新手。 I have a MYSQL table "customer" with data as shown: Data in the table When testing the API output using Postman, there seems to be rows of empty JSON output.我有一个带有数据的MYSQL表“客户”,如下所示:表中的数据使用Postman测试API输出时,似乎会有空JSON输出行。

API Output API输出

Below is my code:下面是我的代码:

package com.semika.customer;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer") 
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

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

    public Customer() {
       super();

    }

}

CustomerRepository客户资料库

package com.semika.customer;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long>{

}

CustomerService客户服务

package com.semika.customer;

public interface CustomerService {
    public Iterable<Customer> findAll(); 
}

CustomerServiceImpl CustomerServiceImpl

package com.semika.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService{

    @Autowired
    private CustomerRepository customerRepository;

    public Iterable<Customer> findAll() {
        return customerRepository.findAll(); 
    }
}

CustomerController客户控制器

package com.semika.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService  customerService;

    @RequestMapping("/customers") 
    @ResponseBody
    public Iterable<Customer> findAll() {
       Iterable<Customer> customers = customerService.findAll();
       return customers;
    }
}

I don't know what else I need to modify in the controller to be able to see the output with data.我不知道我还需要在控制器中修改什么才能看到带有数据的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM