【问题标题】:Spring Data Rest Unexpected behavior on GETSpring Data Rest GET 上的意外行为
【发布时间】:2018-03-02 06:27:57
【问题描述】:

我在一个带有 Spring Data Rest 的 Spring Boot 项目中,我已经实现了一个获取数据休息的方法,并且它的工作正常:

@CrossOrigin
@RepositoryRestResource(collectionResourceRel="categorias-perguntas",path="categorias-perguntas")
public interface CategoriaPerguntaRepository extends CrudRepository<CategoriaPergunta, Long>{}

在我实现了一个普通的 RestController 之后:

@CrossOrigin
@RestController("motoristas")
public class MotoristaController {...}

一次获得:

@GetMapping
@ResponseStatus(code=HttpStatus.OK)
@ResponseBody
public Motorista buscaMotoristaPeloUsuario(@RequestParam(value="idUsuario") Long idUsuario) {
    return this.motoristaService.findByUsuarioId(idUsuario);
}

实现这个控制器后,我的 DataRest "/categorias-perguntas" 已经停止工作,并返回:

"error": "Bad Request",
"exception": "org.springframework.web.bind.MissingServletRequestParameterException",
"message": "Required Long parameter 'idUsuario' is not present",
"path": "/categorias-perguntas"

但是“/categorias-perguntas”没有这个参数。 为什么我的 RestController 改变了 Data Rest Resource 的行为?

【问题讨论】:

  • 尝试使用 URL - /categorias-perguntas?idUsuario=1L 访问您的后端。

标签: spring spring-data-rest spring-restcontroller spring-rest


【解决方案1】:

我建议为您的MotoristaController f.e. 添加一些路径。通过添加类级别注释 @ResquestMapping('/motorista')` 并尝试一下。

【讨论】:

    【解决方案2】:

    我从@ResquestMapping 注释中解决了这个问题,并将路径放在@GetMapping("motoristas") 上。但我不知道为什么会出现问题。

    【讨论】:

      猜你喜欢
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2018-08-02
      • 2017-05-08
      • 2021-10-02
      • 2015-07-14
      • 1970-01-01
      相关资源
      最近更新 更多