推断出的类型 ‘S’ 不在其界限内;应该扩展 ‘com.javatest.entity.User’。

huangapple go评论84阅读模式
英文:

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 = &quot;users&quot;)
public class UserController {
@Autowired
private UserRepository repository;
@GetMapping
public Iterable&lt;User&gt; findAll() {
return repository.findAll();
}
@GetMapping(path = &quot;/{username}&quot;)
public User find(@PathVariable(&quot;username&quot;) String username) {
return repository.findOne(username);  \\error here
}
@PostMapping(consumes = &quot;application/json&quot;)
public User create(@RequestBody User user) {
return repository.save(user);
}
@DeleteMapping(path = &quot;/{username}&quot;)
public void delete(@PathVariable(&quot;username&quot;) String username) {
repository.delete(username);\\error
}
@PutMapping(path = &quot;/{username}&quot;)
public User update(@PathVariable(&quot;username&quot;) 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不是TUser)的类型/子类型,因此会出现错误。

要修复这个问题,你需要在你的JpaRepository下定义一个名为findByUsername的方法。对其他方法也要进行类似的操作。

英文:

You calling most of your methods under the intefaceQueryByExampleExecutor&lt;T&gt;, where the method parameter is typically of type Example. For example, findOne() is defined as

&lt;S extends T&gt; Optional&lt;S&gt; findOne(Example&lt;S&gt; 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.

huangapple
  • 本文由 发表于 2020年7月27日 19:11:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定