简体   繁体   English

Spring Boot API返回不带标签的json

[英]Spring Boot API returns json without labels

I'm building a rest API with Java Spring Boot and I'm running into a problem, I have the following class with a method (which is in my controller for testing purposes, I will send its logic to the service later): 我正在使用Java Spring Boot构建一个Rest API,遇到一个问题,我在下面的类中使用了一种方法(出于测试目的,该类在我的控制器中,稍后将其逻辑发送给服务):

@RestController
@RequestMapping("/api/readings")
public class Readings {
    @Autowired
    private ReadingService readingService;

    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public List<Reading> getRelevant(@RequestParam("start") String start, @RequestParam("end") String end){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
        start += " 00:00:00";
        end += " 23:59:59";
        try {
            Date startdate = df.parse(start);
            Date enddate = df.parse(end);
            return readingService.getRelevant(startdate, enddate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}

This makes use of a service that calls the following repository function: 这利用了调用以下存储库功能的服务:

@Query("SELECT pmtwofive, pmten, recording FROM Reading WHERE recording >= ?1 AND recording <= ?2")
List<Reading> getRelevant(Date start, Date end);

Everything works fine, except for the format of the result: 一切工作正常,除了结果的格式:

[[10,20,1505801743816],[14,21,1505802311976],[14,21,1505802330610],[10,13,1505803302960],[10,13,1505803321966]]

Instead of this, I was expecting something like I get when using the CrudRepository from hibernate querying my whole table instead of just these three values: 取而代之的是,我期望从休眠状态查询整个表而不是这三个值时使用CrudRepository时会得到类似的结果:

{
    {
        pmtwofive: 10,
        pmten: 20,
        reading: 1505801743816
    },
    {
        ...
    }
}

What should I do to get my expected result? 我应该怎么做才能获得预期的结果? Thank you! 谢谢!

Reading Class:

package com.amione.models;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
public class Reading {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private long reading_id;

    private int sensor_id;
    private int function;
    private int directionstart;
    private int pmtwofive;
    private int pmten;
    private int checksumlow;
    private int checksumhigh;
    private Timestamp recording;

    public long getReading_id() {
        return reading_id;
    }

    public void setReading_id(int reading_id) {
        this.reading_id = reading_id;
    }

    public int getSensor_id() {
        return sensor_id;
    }

    public void setSensor_id(int sensor_id) {
        this.sensor_id = sensor_id;
    }

    public int getFunction() {
        return function;
    }

    public void setFunction(int function) {
        this.function = function;
    }

    public int getDirectionstart() {
        return directionstart;
    }

    public void setDirectionstart(int directionstart) {
        this.directionstart = directionstart;
    }

    public int getPmtwofive() {
        return pmtwofive;
    }

    public void setPmtwofive(int pmtwofive) {
        this.pmtwofive = pmtwofive;
    }

    public int getPmten() {
        return pmten;
    }

    public void setPmten(int pmten) {
        this.pmten = pmten;
    }

    public int getChecksumlow() {
        return checksumlow;
    }

    public void setChecksumlow(int checksumlow) {
        this.checksumlow = checksumlow;
    }

    public int getChecksumhigh() {
        return checksumhigh;
    }

    public void setChecksumhigh(int checksumhigh) {
        this.checksumhigh = checksumhigh;
    }

    public Timestamp getRecording() {
        return recording;
    }

    public void setRecording(Timestamp recording) {
        this.recording = recording;
    }
}

Ok I have the answer. 好吧,我有答案。 It must be done with custom constructor: 必须使用自定义构造函数完成:

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
public class City extends AbstractEntity {

@Column
private String name;

@Embedded
private Geopoint centre=new Geopoint();

public City(){}
public City(String name){
    this.setName(name);
    }
//  @OneToMany(mappedBy="city")
//  private Set<Place> places;

}

Repository: 库:

public interface CityRepository extends JpaRepository<City, Long>{

    City findOneByName(String name);
    @Query("SELECT name FROM City")
    public List<City> findMethod1();

    @Query("SELECT c.name FROM City c")
    public List<City> findMethod2();

Controller: 控制器:

@Autowired
private CityRepository cityRepository;
@GetMapping("/test")
public List<City> test(){
    List<City> ret=new ArrayList();
    ret.addAll(cityRepository.findMethod1());
    ret.addAll(cityRepository.findMethod2());
    ret.addAll(cityRepository.findMethod3());
    return ret;
}

and the result: 结果:

在此处输入图片说明

As you can see, 3rd method works. 如您所见,第3种方法有效。 I told you it will came up. 我告诉过你它会来的。

As empty values are still serialized, you can use a DTO object to encapsule only required fields (and SELECT new EntityDTO(field1,field2,field3) FROM Entity ) 由于仍然对空值进行序列化,因此您可以使用DTO对象封装仅必需的字段(并SELECT new EntityDTO(field1,field2,field3) FROM Entity

Another option would bo to configure Jackson to not to serialize null values with annotation or configuratio, but that is just beyond the scope of question. 另一个选择是将杰克逊配置为不使用注释或配置序列化空值,但这超出了问题的范围。

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

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