现在我的项目已成功完成,我们正在尝试记录所学的经验教训。仍然让我感到困惑的是:
我们有一个地址数据库,当用户开始输入街道名称时需要自动填充。使用JPA存储库,我们实现了一个PString类(只是String的持久包装器),然后实现了这个接口:
@RepositoryRestResource(collectionResourceRel = "locations", path = "locations")
public interface LocationRepository extends JpaRepository<Location, Integer>, LocationRepositoryCustom {
List<Location> findByStreetNameAndCommunity_ID(@Param("street") String streetName, @Param("commId") Integer commId);
@Modifying
@Query("select distinct x.streetName from Location x where x.streetName like :street%")
List<PString> findStreetNameStartingWith(@Param("street") String streetName);
}
尝试通过网络调用位置/ search / findStreetNameStartingWith?street = N%20College导致:
{&#34;原因&#34;:null,&#34;消息&#34;:&#34; PersistentEntity不能为空!&#34;}
但是,我们添加了一个控制器来调用方法:
@RestController
@RequestMapping("/custom/locations")
public class LocationController {
@Autowired
private LocationRepository repo;
@RequestMapping(value = "/findStreetNamesStartingWith", method=RequestMethod.GET)
public List<PString> findStreetNameStartingWith(
@Param("streetName") String streetName) {
return repo.findStreetNameStartingWith(streetName);
}
}
调用/ custom / locations / findStreetNamesStartingWith?streetName = N%20Coll返回预期的三个结果。如果直接调用该方法为什么不起作用,但是当我们通过控制器管道时它像greyhound一样运行?
答案 0 :(得分:0)
确保正确配置Spring Data REST,例如添加RepositoryRestConfiguration
:
@Configuration
public class CustomizedRestMvcConfiguration extends RepositoryRestMvcConfiguration {
@Override
public RepositoryRestConfiguration config() {
RepositoryRestConfiguration config = super.config();
config.setBasePath("/custom");
return config;
}
}