简体   繁体   English

弹簧架控制器

[英]Spring Rest Controller

I can't make spring serialize the response when results is array/list . 当结果为array / list时,我无法使spring序列化响应。 So when I call clients from RestController it does return [{},{},{}] , instead of real objects, all other methods works just fine. 因此,当我从RestController调用clients ,它确实返回[{},{},{}] ,而不是实际对象,所有其他方法都可以正常工作。

package com.test.Domain.Client;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;    
import javax.persistence.Table;
import java.util.UUID;

@Entity
@Table(name = "client")
public class Client {

    @Column(name = "client_id")
    @Id
    private UUID clientId;
    @Column(name = "name")
    private String name;

    private Client() {

    }

    private Client(UUID clientId, String name) {

        this.clientId = clientId;
        this.name = name;
    }

    public static Client create(String name)
    {
        return new Client(UUID.randomUUID(), name);
    }
}

package com.test.Rest;

import com.test.Domain.Calendar.AppointmentRepository;
import com.test.Domain.Client.Client;
import com.test.Domain.Client.ClientRepository;
import com.test.Domain.Worker.WorkerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@org.springframework.web.bind.annotation.RestController
public class RestController {

    @Autowired
    private ClientRepository clientRepository;
    @Autowired
    private WorkerRepository workerRepository;
    @Autowired
    private AppointmentRepository appointmentRepository;


    @RequestMapping(path = "/client", method = RequestMethod.POST)
    public void registerClient(@RequestParam(name = "name") String name) {
        this.clientRepository.save(Client.create(name));
    }

    @RequestMapping(path = "/clientCount", method = RequestMethod.GET)
    public Long countClient() {
        return this.clientRepository.count();
    }

    @RequestMapping(path = "/clients", method = RequestMethod.GET)
    @ResponseBody
    public List<Client> clients() {
        List<Client> list = new ArrayList<Client>();
        for (Client client : this.clientRepository.findAll()) {
            list.add(client);
        }

        return list;
    }
}

Jackson needs Getter and Setter methods in order to serialize the Client object properly into JSON. 为了将Client对象正确序列化为JSON,Jackson需要Getter和Setter方法。 Therefore a list of empty objects is returned and the values for the members are missing. 因此,将返回一个空对象列表,并且缺少成员的值。 Add them to Client and the response should look fine. 将它们添加到Client ,响应应该看起来不错。

Spring applies first registered applicable by response mime-type HttpMessageConverter implementation when serializing the response to /clients call. 当将响应序列化为/clients调用时,Spring首先应用响应mime类型的HttpMessageConverter实现注册的响应。 In your case this is some JSON serializer. 在您的情况下,这是一些JSON序列化器。 As you have no JSON configuration specified on Client class the default POJO serializing approach is used: reflection scanning of object properties. 由于没有在Client类上指定JSON配置,因此将使用默认的POJO序列化方法:对象属性的反射扫描。 As mentioned earlier your Client class doesn't define any properties (at least getters), so serializer do not detect any. 如前所述,您的Client类未定义任何属性(至少是getter),因此序列化程序不会检测到任何属性。

Please refer to the following article for a more detailed explanation: https://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html 请参阅以下文章以获取更详细的说明: https : //www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html

PS Marking method with @ResponseBody in @RestController annotated class is not necessary as itself is a convenience annotation aggregating @Controller and @ResponseBody . 与PS标记方法@ResponseBody@RestController作为本身是一个方便的注解注解聚集类是没有必要的@Controller@ResponseBody

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

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