我正在尝试从https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web.basic(基本网络支持)
重现代码示例我在Spring Web MVC应用程序中有一些控制器:
@Controller // This means that this class is a Controller
@RequestMapping(path = "/demo") // This means URL's start with /demo (after Application path)
@EnableSpringDataWebSupport
public class MainController {
@GetMapping(path = "/getResById/{id}")
@ResponseBody
public Tresource getResById(@PathVariable("id") Tresource tr, Model m) {
m.addAttribute(tr);
return tr;
}
}
应用程序主条目是具有@SpringBootApplication
注释的类。
我也有存储库类:
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public interface UserRepository extends Repository<Tresource, Long> {
// List<Tresource> findByBrief(String brief);
@Query("Select t.brief from Tresource t where t.resourceId=:resourceId")
String qqq(@Param("resourceId") Long resourceId);
Optional<Tresource> findDistinctByResourceIdOrBrief(Long resourceId, String brief);
@Query("Select i from Tresource t "
+ "inner join Tinstitution i on i.institutionId=t.instOwnerId "
+ "where t.resourceId=:resourceId")
Optional<Tinstitution> getResOwner(@Param("resourceId") Long resourceId);
}
此代码获取异常
2018-03-21 14:33:15.977 WARN 11464 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.mycompany.eurofatcafns.db.Tresource'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.PathVariable com.mycompany.eurofatcafns.db.Tresource] for value '2010015161038'; nested exception is java.lang.IllegalStateException: Repository doesn't have a find-one-method declared!
当我访问/ demo / getResById / 2010015161038时。 这段代码我做错了什么?如何修复此代码?
非常感谢!
答案 0 :(得分:1)
找到解决方案!
在我的代码示例中,我使用了
public interface UserRepository extends Repository<Tresource, Long> {
但getOne(ID)
和findOne(ID)
位于CrudRepository
类。
所以,我将代码修改为此代码,现在一切正常:
public interface UserRepository extends CrudRepository<Tresource, Long> {