【问题标题】:Spring Data Rest executes query but returns 500 internal Server ErrorSpring Data Rest 执行查询但返回 500 内部服务器错误
【发布时间】:2015-06-16 20:59:19
【问题描述】:

我正在使用 spring boot 和 spring data rest,我面临 500 内部服务器错误,但控制台中没有显示任何消息。

我有以下几点:

ProdutoVendaRepository.java

public interface ProdutoVendaRepository extends PagingAndSortingRepository<ProdutoVenda, Integer> {

@Query("SELECT new br.com.contoso.model.VendaPorFamilia(b.nome, SUM(i.valorMultiplicado)) FROM ProdutoVenda i JOIN i.produto o JOIN o.familia b WHERE b.nome LIKE 'foo' GROUP BY b.nome")
List <VendaPorFamilia> teste();
}

VendaPorFamilia.java

public class VendaPorFamilia {

private String nome;

private double valorTotal;

public VendaPorFamilia(String nome, double valorTotal) {
    this.nome = nome;
    this.valorTotal = valorTotal;
}

//getters and setters

ProdutoVenda.java

@Entity
@Immutable
@Table(name = "INV1")
public class ProdutoVenda {

@Id
@Column(name = "LineNum")
private int id;

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

@Column(name = "Quantity")
private double quantidade;

@Column(name = "Price")
private double precoUnitario;

@Column(name = "LineTotal")
private double valorMultiplicado;

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

@OneToOne
@JoinColumn(name = "ItemCode", updatable = false, insertable = false)
private Produto produto;

//getters and setters

当我尝试时

curl -v  -X GET -H "X-AUTH-TOKEN: TokenFoo"  
http://localhost:8080/api/produtoVendas/search/teste

日志显示查询已执行:

2015-06-16 17:05:45.884 DEBUG 7892 --- [http-nio-8080-exec-1]         org.hibernate.SQL                        
: select familia2_.ItmsGrpNam as col_0_0_, sum(produtoven0_.LineTotal) as col_1_0_ from 
INV1 produtoven0_ inner join OITM produto1_ on produtoven0_.ItemCode=produto1_.ItemCode 
inner join OITB familia2_ on produto1_.ItmsGrpCod=familia2_.ItmsGrpCod where    
familia2_.ItmsGrpNam like 'foo' group by familia2_.ItmsGrpNam

但我收到一个:

< HTTP/1.1 500 Internal Server Error
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Tue, 16 Jun 2015 20:05:46 GMT
< Connection: close
< 
* Closing connection 0

我不知道该怎么做,我尝试了很多东西,但似乎没有任何效果。 是的,当我在 MsSQL 服务器中执行完全相同的生成查询时,它会返回所需的值。 所有其他存储库都可以正常工作...

感谢您的宝贵时间和帮助!

编辑:调试日志显示以下内容:

2015-06-17 15:25:58.670 DEBUG 8667 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/api/produtoVendas/search/teste]
2015-06-17 15:25:58.674 DEBUG 8667 --- [http-nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /api/produtoVendas/search/teste
2015-06-17 15:25:58.678 DEBUG 8667 --- [http-nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/api/produtoVendas/search/teste]
2015-06-17 15:25:58.692 DEBUG 8667 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/api/produtoVendas/search/teste] is: -1
2015-06-17 15:25:58.758 DEBUG 8667 --- [http-nio-8080-exec-1] org.hibernate.SQL                        : select familia2_.ItmsGrpNam as col_0_0_, sum(produtoven0_.LineTotal) as col_1_0_ from INV1 produtoven0_ inner join OITM produto1_ on produtoven0_.ItemCode=produto1_.ItemCode inner join OITB familia2_ on produto1_.ItmsGrpCod=familia2_.ItmsGrpCod where familia2_.ItmsGrpNam like ‘foo’ group by familia2_.ItmsGrpNam
2015-06-17 15:25:58.836 DEBUG 8667 --- [http-nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public org.springframework.http.ResponseEntity<java.lang.Object> org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.web.context.request.WebRequest,java.lang.String,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler)]: java.lang.IllegalArgumentException: PersistentEntity must not be null!
2015-06-17 15:25:58.839 DEBUG 8667 --- [http-nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Invoking @ExceptionHandler method: org.springframework.http.ResponseEntity<org.springframework.data.rest.webmvc.support.ExceptionMessage> org.springframework.data.rest.webmvc.RepositoryRestExceptionHandler.handleMiscFailures(java.lang.Exception)
2015-06-17 15:25:58.856 DEBUG 8667 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2015-06-17 15:25:58.857 DEBUG 8667 --- [http-nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request

【问题讨论】:

    标签: java spring rest spring-boot spring-data-rest


    【解决方案1】:

    在与数据库建立连接的那一行放置一个调试器。

    【讨论】:

    • 嘿,谢谢!我设法记录了一些调试级别并使用新信息编辑了我的问题,有什么想法吗?
    • 我对此并不完全确定,但是已经定义了存储库,扩展了 PagingAndSortingRepository 并期望查询的结果是除 ProdutoVenda 之外的其他类,我不认为它是允许的。
    • 其实你可以,@SalvadorJuanMartinez,详情请看我的回答
    【解决方案2】:

    哼... 这很奇怪,但我发现了这个 question 并添加了一个控制器来调用该方法,它就像一个魅力......

    Obv 这不是一个修复方法,但它是一个很好的解决方法...

    编辑:错误发生是因为它需要一个实体,所以我所要做的就是用 @Entity 注释 VendaPorFamilia。

    【讨论】:

    • 想想还是有道理的,因为Spring Data Rest(JPA)依赖于Hibernate,而Hibernate是一个ORM(Object Relational Mapping),实体是hibernate处理的对象。所以用@Entity注释一个类是“告诉”Hibernate的“方式”,嘿,你有这个类可用于你的操作。
    猜你喜欢
    • 2014-03-25
    • 2018-09-30
    • 1970-01-01
    • 2017-06-29
    • 2017-07-26
    • 1970-01-01
    • 2021-11-12
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多