英文:
Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'com.javatest.entity.User'
问题
我在我的UserController中遇到了问题,当我想在UserController中实现findOne方法时,它告诉我:“类型参数'S'的推断类型'S'不在其限制范围内;应该扩展'javatest.entity.User'。
我想自己修复,但我不明白这是什么意思。你能帮我解决这个问题吗?
谢谢
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.javatest.entity.User;
import com.javatest.repository.UserRepository;
@RestController
@RequestMapping(path = "users")
public class UserController {
@Autowired
private UserRepository repository;
@GetMapping
public Iterable<User> findAll() {
return repository.findAll();
}
@GetMapping(path = "{username}")
public User find(@PathVariable("username") String username) {
return repository.findOne(username); // 在这里出错
}
@PostMapping(consumes = "application/json")
public User create(@RequestBody User user) {
return repository.save(user);
}
@DeleteMapping(path = "{username}")
public void delete(@PathVariable("username") String username) {
repository.delete(username); // 在这里出错
}
@PutMapping(path = "{username}")
public User update(@PathVariable("username") String username, @RequestBody User user) {
if (repository.exists(username)) {
user.setUsername(username); // 在这里出错
return repository.save(user);
}
}
}
英文:
I have a problem with my UserController,when I want realize method findOne in UserController it tells me "Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'javatest.entity.User".
I wanted to fix by myself, but I don't understand what this means. Could you please help me with this issue.
Thanks
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.javatest.entity.User;
import com.javatest.repository.UserRepository;
@RestController
@RequestMapping(path = "users")
public class UserController {
@Autowired
private UserRepository repository;
@GetMapping
public Iterable<User> findAll() {
return repository.findAll();
}
@GetMapping(path = "/{username}")
public User find(@PathVariable("username") String username) {
return repository.findOne(username); \\error here
}
@PostMapping(consumes = "application/json")
public User create(@RequestBody User user) {
return repository.save(user);
}
@DeleteMapping(path = "/{username}")
public void delete(@PathVariable("username") String username) {
repository.delete(username);\\error
}
@PutMapping(path = "/{username}")
public User update(@PathVariable("username") String username, @RequestBody User user) {
if (repository.exists(username)) {
user.setUsername(username); \\error
return repository.save(user);
}
}
}
}
答案1
得分: 2
你在接口QueryByExampleExecutor<T>
下调用大多数方法,其中方法参数通常是Example
类型。例如,findOne()
的定义如下:
<S extends T> Optional<S> findOne(Example<S> example);
而你正在将username
作为String
传入,这不是一个泛型类型,在你的情况下,S
不是T
(User
)的类型/子类型,因此会出现错误。
要修复这个问题,你需要在你的JpaRepository
下定义一个名为findByUsername
的方法。对其他方法也要进行类似的操作。
英文:
You calling most of your methods under the intefaceQueryByExampleExecutor<T>
, where the method parameter is typically of type Example
. For example, findOne()
is defined as
<S extends T> Optional<S> findOne(Example<S> example);
and you are passing in username as String
, which isn't a generic type where S
is a type/subtype of T
(User
) in your case, hence the error.
To fix, you want to define a method called findByUsername
under your JpaRepository
. Do so for other methods as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论