Spring mvc restful - 错误的json回复格式

时间:2014-01-26 13:32:39

标签: java spring spring-mvc jpa spring-data-jpa

回复ajax请求时遇到一些问题。最初,我有最简单的restful服务,基于 spring boot MVC

模型:

import javax.persistence.*; 
import java.util.*;

@Entity 
@Table(name = "testmodel")
public class TestModel 
{
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)//Postgresql sequence generator 
    private long id;

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

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


    //Constructor
    public TestModel()
    {   
    }

    //Id getter
    public long getId()
    {
        return this.id;
    }

    //Name getter-setter
    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    //Content getter-setter
    public String getContent()
    {
        return this.content;
    }

    public void setContent(String content)
    {
        this.content = content;
    }
}

模型的DAO:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.*;

public interface TetsModelDAO extends JpaRepository<Samples, Long> 
{
    @Query("SELECT s FROM TestModel s WHERE LOWER(s.name) LIKE LOWER(:entry) ORDER BY s.name")
    List<TestModel> fetchByNameEntry(@Param("entry") String entry);
}

控制器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/")
public class TestController 
{
    @Autowired
    private TetsModelDAO testmodel;

    @RequestMapping("/name")
    public List<TestModel> getDatasetsByNameEntry(@RequestParam("entry") String entry) 
    {
        return testmodel.fetchByNameEntry("%"+entry+"%");
    }
}

客户端ajax请求:

$.ajax(
{ 
  url : "/name?entry=", //get all records
  method: "GET"
})

此示例效果很好 - 字符串化的回复看起来像标准 json 结构:

{"id":"1", "name":"John", "content":"blablabla1"}
{"id":"2", "name":"Sam", "content":"blablabla2"}
{"id":"3", "name":"Ken", "content":"blablabla3"}

但是,当我尝试在JPQL查询中明确定义文件时(例如,仅提取 id 名称字段),我得到错误的回复结果。

带有修改查询的DAO(其他代码没有更改):

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.*;

public interface TetsModelDAO extends JpaRepository<Samples, Long> 
{
   @Query("SELECT s.id, s.name FROM TestModel s WHERE LOWER(s.name) LIKE LOWER(:entry) ORDER BY s.name")
   List<TestModel> fetchByNameEntry(@Param("entry") String entry);
}

在这种情况下,回复看起来像这样:

1, John, 2, Sam, 3, Ken

如何优雅地解决这个问题(不创建“助手类”)?

2 个答案:

答案 0 :(得分:2)

您可以直接从存储库返回DTO:

public interface TetsModelDAO extends JpaRepository<Samples, Long> 
{
   @Query("SELECT new mypackage.TestDto(s.id, s.name) FROM TestModel s WHERE LOWER(s.name) LIKE LOWER(:entry) ORDER BY s.name")
   List<TestDto> fetchByNameEntry(@Param("entry") String entry);
}

其中TestDto仅包含必填字段:

package mypackage;

public class TestDto {
  private final long id;
  private final String name;

  public TestDto(long id, String name) {
    this.id = id;
    this.name = name;
  }

  public long getId() {
    return id;
  }

  public String getName() {
    return name;
  }
}

答案 1 :(得分:0)

您的查询不会返回TestModel的实例。它返回对象数组(即List<Object[]>),每个数组包含ID和找到的TestModel的名称。正确的查询是

SELECT s FROM TestModel s WHERE LOWER(s.name) LIKE LOWER(:entry) ORDER BY s.name

您最好实施自动化测试,以检查您的DAO查询是否返回了应有的内容。