如何获得仅包含特定对象的json的休息响应?

时间:2016-06-25 13:19:30

标签: json rest spring-boot response

我的目标是让休息服务在Spring Boot Web应用程序上运行。 但我正在努力寻找如何只对包含对象的json做出回应。

更准确地说,当调用http://localhost:8080/api/db/Keyboard/2时,我打算以json格式接收id为2的对象,并且只有:

{
  "id": 2,
  "language": "en"
}

但我得到了:

{
  "status": 200,
  "entity": {
    "id": 2,
    "language": "en"
  },
  "metadata": {},
  "length": -1,
  "allowedMethods": [],
  "cookies": {},
  "headers": {},
  "actualEntity": {
    "id": 2,
    "language": "en"
  },
  "links": [],
  "statusInfo": {
    "reasonPhrase": "OK",
    "statusCode": 200,
    "family": "SUCCESSFUL"
  },
  "stringHeaders": {}
}

显然,回复包含太多信息。只需要实体部分。如何达到预期结果/有条件地调整响应?

下面是一些可能相关的文件。

TestController.java:

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

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

import main.domain.DaoApi;
import main.domain.Keyboard;

@RestController
public class TestController<T, K> {

    private final static Map<String, Class<?>> tableClassMap = new HashMap<>();

    @Autowired
    private DaoApi<T, K> daoApi;

    static {
        addEntryTableClassMap(Keyboard.class);
    }

    @RequestMapping(value = "/api/db/{tableName}/{id}", method = RequestMethod.GET)
    public Response getById(@PathVariable(value = "tableName") String tableName, @PathVariable(value = "id") Integer id) {
        ResponseBuilder responseBuilder;
        T entity = (T) daoApi.findById((Class<T>) getClassFromTableClassMap(tableName), id);
        if (entity != null) {
            responseBuilder = Response.ok(entity);
        } else {
            responseBuilder = Response.status(Status.NOT_FOUND);
        }
        return responseBuilder.build();
    }

    private static <C> void addEntryTableClassMap(Class<C> clazz) {
        tableClassMap.put(clazz.getSimpleName().toLowerCase(), clazz);
    }

    private static <C> Class<C> getClassFromTableClassMap(String tableName) {
        return (Class<C>) tableClassMap.get(tableName.toLowerCase());
    }
}

Keyboard.java:

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

import org.springframework.transaction.annotation.Transactional;

@Transactional
@Entity
@Table(name = "keyboard")
public class Keyboard {

    @Id
    @Column(updatable = false)
    private int id;
    private String language;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
}

DaoApi.java:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DaoApi<T, K> {

    @Autowired
    SessionFactory sessionFactory;

    public T findById(Class<T> clazz, Integer id) {

        Session session = sessionFactory.openSession();
        T t = (T) session.get(clazz, id);
        session.close();
        return t;
    }
}

1 个答案:

答案 0 :(得分:0)

收到comment帮我解决问题:

@RequestMapping(value = "/api/db/{tableName}/{id}", method = RequestMethod.GET)
public T getById(@PathVariable(value = "tableName") String tableName, @PathVariable(value = "id") Integer id) {
    ResponseBuilder responseBuilder;
    T entity = (T) daoApi.findById((Class<T>) getClassFromTableClassMap(tableName), id);
    if (entity != null) {
        responseBuilder = Response.ok(entity);
        return (T) responseBuilder.build().getEntity();
    } else {
        responseBuilder = Response.status(Status.NOT_FOUND);
        //some additional code here
        return (T) responseBuilder.build();
    }
}