自定义RepositoryRestController映射网址在spring-data-rest中抛出404 [英] Custom RepositoryRestController Mapping url throws 404 in spring-data-rest

查看:181
本文介绍了自定义RepositoryRestController映射网址在spring-data-rest中抛出404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用其相应的实体存储库编写了一个自定义的 RepositoryRestController 。在此url上执行请求时,查询正在我的控制台中运行,但是url返回404。我还能够在日志中看到此url的requestHandlerMapping。请参阅我的以下代码段。

I have written a custom RepositoryRestController using its corresponding entity repository. When performing request on this url, the query is running in my console, but the url returns 404. I also able to see the requestHandlerMapping for this url in logs. Refer my following code snippet.

存储库:

   @RepositoryRestResource
    public interface FooRepository extends BaseRepository<Foo, Integer> {
    @RestResource(exported = false)
    List<Foo> findByName(String name);
    }

控制器:

@RepositoryRestController
public class FooResource {
@Aurowired
FooRepository fooRepository;

@Autowired
RestApiService restApiService;

@RequestMapping(value = "/foos/search/byApiName", method = GET)
public ResponseEntity<?> findByApiName(String name) {
List<String> apiNames = restApiService.getNameLike(name);
List<Foo> fooList = fooRepository.findByName(name);

List<String> fooNames = // list of names from fooList
...
System.out.println("FETCHING apiNames");
return apiNames;
}

当我执行以下curl命令时

When I execute the following curl command

curl -X GET http:localhost:8080/foos/search/byApiName

响应返回404错误。我不知道为什么

the response returns 404 error. I don't know why. But he printout statement is printing in console.

@hat我在这里做错了吗?

@hat I am doing wrong here?

推荐答案

我建议您将 @ResponseBody 添加到您的方法或返回值中(如SDR示例 http:// docs。 spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers ),或将您的列表包装在ResponseEntity中。

I would suggest that you need to add the @ResponseBody to either your method or to the return value (as in the SDR example http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers) or wrap your list in an ResponseEntity.

不确定这些方法之间是否有任何区别,但都应该如Spring MVC文档中所述:

Not sure if there are any differences between these approaches but all should, as noted in the Spring MVC docs:

表示返回类型应直接写入HTTP
响应正文中(而不是放置在Model中,或[be]解释为视图
名称)

"indicate that the return type should be written straight to the HTTP response body (and not placed in a Model, or [be] interpreted as a view name)."

http://docs.spring.io/spring/docs/current/spring-framework-reference/html /mvc.html#mvc-ann-responsebody

鉴于您调试了语句打印,但是得到了404,看来后面的动作是会发生什么。 / p>

Given you debug statement prints but you get a 404 it would seem the latter action is what happens.

@RequestMapping(value = "/foos/search/byApiName", method = GET)
@ResponseBody
public ResponseEntity<?> findByApiName(String name) {
    // ....
}

@RequestMapping(value = "/foos/search/byApiName", method = GET)
public @ResponseBody ResponseEntity<?> findByApiName(String name) {
   //....
}

或者,

@RequestMapping(value = "/foos/search/byApiName", method = GET)
public ResponseEntity<List<String>> findByApiName(String name) {
   //....

  return new ResponseEntity<List<String>>(fooNames, HttpStatus.OK);
}

这篇关于自定义RepositoryRestController映射网址在spring-data-rest中抛出404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆