Spring-Data Mysql在数据库中找不到该名称。

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

Spring-Data Mysql cannot find the name in the DB

问题

  1. 我对Spring-Data MySQL还不太熟悉我创建了一个小方法让它从一个名字列表中返回一个名称它会返回具有最旧的记录日期的名称不幸的是当我执行获取操作时它返回给我这个

D:>curl -G localhost:8080/demo/first -d name=Biagio
{"timestamp":"2020-10-02T09:29:33.704+00:00","status":404,"error":"Not Found","message"

  1. 我已经尝试了各种方法,但到目前为止都没有成功。以下是该方法和相关控制器。
  2. UserRepositoryImpl.java
  3. ```java
  4. package com.example.accessingdatamysql;
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.TypedQuery;
  7. import javax.persistence.criteria.CriteriaBuilder;
  8. import javax.persistence.criteria.CriteriaQuery;
  9. import javax.persistence.criteria.Root;
  10. import org.springframework.stereotype.Component;
  11. @Component
  12. public class UserRepositoryImpl implements UserRepository {
  13. private final EntityManager em;
  14. public UserRepositoryImpl(EntityManager entityManager) {
  15. this.em = entityManager;
  16. }
  17. @Override
  18. public User findFirstByName(String name) {
  19. CriteriaBuilder builder = em.getCriteriaBuilder();
  20. CriteriaQuery<User> criteria = builder.createQuery(User.class);
  21. Root<User> root = criteria.from(User.class);
  22. criteria.select(root).where(builder.equal(root.get("name"), name));
  23. criteria.orderBy(builder.asc(root.get("timestamp")));
  24. TypedQuery<User> query = em.createQuery(criteria).setMaxResults(1);
  25. return query.getSingleResult();
  26. }
  27. @Override
  28. public void create(User entity) {
  29. em.persist(entity);
  30. }
  31. }

MainController.java

  1. package com.example.accessingdatamysql;
  2. import javax.persistence.NoResultException;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. @Controller
  12. @RequestMapping(path="/demo")
  13. public class MainController {
  14. @Autowired
  15. private UserRepository userRepository;
  16. @Transactional
  17. @PostMapping(path="/add")
  18. public @ResponseBody String addNewUser (@RequestParam String name, @RequestParam String email, @RequestParam String surname) {
  19. User n = new User();
  20. n.setName(name);
  21. n.setSurname(surname);
  22. n.setEmail(email);
  23. userRepository.create(n);
  24. return "Saved";
  25. }
  26. @GetMapping("/first")
  27. User one(@RequestParam String name) {
  28. System.out.print(name);
  29. try {
  30. return userRepository.findFirstByName(name);
  31. } catch (NoResultException nre) {
  32. return null;
  33. }
  34. }
  35. }

还有User和UserRepository.java

User.java

  1. package com.example.accessingdatamysql;
  2. import java.sql.Timestamp;
  3. import java.time.Instant;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. @Entity
  10. public class User {
  11. @Id
  12. @GeneratedValue(strategy=GenerationType.AUTO)
  13. private Integer id;
  14. public String name;
  15. private String email;
  16. private String surname;
  17. @Column(name="stmp", columnDefinition = "TIMESTAMP (6)")
  18. Timestamp timestamp = Timestamp.from(Instant.now());
  19. public void setTimestamp(Timestamp timestamp) {
  20. this.timestamp = timestamp;
  21. }
  22. public Timestamp getTimestamp() {
  23. return timestamp;
  24. }
  25. public String getSurname() {
  26. return surname;
  27. }
  28. public void setSurname(String surname) {
  29. this.surname = surname;
  30. }
  31. public Integer getId() {
  32. return id;
  33. }
  34. public void setId(Integer id) {
  35. this.id = id;
  36. }
  37. public String getName() {
  38. return name;
  39. }
  40. public void setName(String name) {
  41. this.name = name;
  42. }
  43. public String getEmail() {
  44. return email;
  45. }
  46. public void setEmail(String email) {
  47. this.email = email;
  48. }
  49. }

UserRepository.java

  1. package com.example.accessingdatamysql;
  2. import org.springframework.stereotype.Repository;
  3. @Repository
  4. public interface UserRepository {
  5. User findFirstByName(String name);
  6. void create(User entity);
  7. }
英文:

I'm new to Spring-Data Mysql, I created a little method to make it give me a name from a list of names, it returns me the name with the oldest entry date. Unfortunately when I do the get it returns me this :

  1. D:\&gt;curl -G localhost:8080/demo/first -d name=Biagio
  2. {&quot;timestamp&quot;:&quot;2020-10-02T09:29:33.704+00:00&quot;,&quot;status&quot;:404,&quot;error&quot;:&quot;Not Found&quot;,&quot;message

I've tried them all a little but so far I can't be successful. Below is the method and the relative controller

UserRepositoryImpl.java

  1. package com.example.accessingdatamysql;
  2. import javax.persistence.EntityManager;
  3. import javax.persistence.TypedQuery;
  4. import javax.persistence.criteria.CriteriaBuilder;
  5. import javax.persistence.criteria.CriteriaQuery;
  6. import javax.persistence.criteria.Root;
  7. import org.springframework.stereotype.Component;
  8. @Component
  9. public class UserRepositoryImpl implements UserRepository {
  10. private final EntityManager em;
  11. public UserRepositoryImpl(EntityManager entityManager) {
  12. this.em = entityManager;
  13. }
  14. @Override
  15. public User findFirstByName(String name) {
  16. CriteriaBuilder builder = em.getCriteriaBuilder();
  17. CriteriaQuery&lt;User&gt; criteria = builder.createQuery(User.class);
  18. Root&lt;User&gt; root = criteria.from(User.class);
  19. criteria.select(root).where(builder.equal(root.get(&quot;name&quot;), name));
  20. criteria.orderBy(builder.asc(root.get(&quot;timestamp&quot;)));
  21. TypedQuery&lt;User&gt; query = em.createQuery(criteria).setMaxResults(1);
  22. return query.getSingleResult();
  23. }
  24. @Override
  25. // per la creazione//
  26. public void create(User entity) {
  27. em.persist(entity);
  28. }
  29. }

mainController.java

  1. package com.example.accessingdatamysql;
  2. import javax.persistence.NoResultException;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. @Controller
  12. @RequestMapping(path=&quot;/demo&quot;)
  13. public class MainController {
  14. @Autowired
  15. private UserRepository userRepository;
  16. @Transactional
  17. @PostMapping(path=&quot;/add&quot;)
  18. public @ResponseBody String addNewUser (@RequestParam String name
  19. , @RequestParam String email,@RequestParam String surname)
  20. {
  21. User n = new User();
  22. n.setName(name);
  23. n.setSurname(surname);
  24. n.setEmail(email);
  25. userRepository.create(n);
  26. return &quot;Saved&quot;;
  27. }
  28. @GetMapping(&quot;/first&quot;)
  29. User one(@RequestParam String name) {
  30. System.out.print(name);
  31. try { return userRepository.findFirstByName(name);
  32. } catch (NoResultException nre) {
  33. return null; }
  34. }
  35. }

I add User and UserRepository.java for everything

User.java

  1. package com.example.accessingdatamysql;
  2. import java.sql.Timestamp;
  3. import java.time.Instant;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. @Entity
  10. public class User {
  11. @Id
  12. @GeneratedValue(strategy=GenerationType.AUTO)
  13. private Integer id;
  14. public String name;
  15. private String email;
  16. private String surname;
  17. @Column(name=&quot;stmp&quot;, columnDefinition = &quot;TIMESTAMP (6)&quot;)
  18. Timestamp timestamp = Timestamp.from(Instant.now());
  19. public void setTimestamp(Timestamp timestamp) {
  20. this.timestamp = timestamp;
  21. }
  22. public Timestamp getTimestamp() {
  23. return timestamp;
  24. }
  25. public String getSurname() {
  26. return surname;
  27. }
  28. public void setSurname(String surname) {
  29. this.surname = surname;
  30. }
  31. public Integer getId() {
  32. return id;
  33. }
  34. public void setId(Integer id) {
  35. this.id = id;
  36. }
  37. public String getName() {
  38. return name;
  39. }
  40. public void setName(String name) {
  41. this.name = name;
  42. }
  43. public String getEmail() {
  44. return email;
  45. }
  46. public void setEmail(String email) {
  47. this.email = email;
  48. }
  49. }

UserRepository.java

  1. package com.example.accessingdatamysql;
  2. import org.springframework.stereotype.Repository;
  3. @Repository
  4. public interface UserRepository {
  5. User findFirstByName(String name);
  6. void create(User entity);
  7. }

EDIT: I insert the database referenced by this small project:

  1. +----+---------------------+--------+----------------------------+---------+
  2. | id | email | name | stmp | surname |
  3. +----+---------------------+--------+----------------------------+---------+
  4. | 32 | mirketto90@yahoo.it | Mirko | 2020-10-01 12:31:47.827000 | NULL |
  5. | 36 | biagio@gmail.com | Biagio | 2020-10-01 16:31:31.687000 | Vaso |
  6. | 37 | biagio@gmail.com | Biagio | 2020-10-01 16:31:50.077000 | Vaso |
  7. | 38 | biagio@gmail.com | Biagio | 2020-10-01 18:35:45.992000 | Vaso |
  8. +----+---------------------+--------+----------------------------+---------+

答案1

得分: 1

我看到的第一件事如下:

  • 你的 /demo/first 方法不是公开的。
  • 调用此端点的正确方法应为 curl localhost:8080/demo/first?name=Biagio -H "Accept: application/json"
英文:

the first things I see are the following:

  • Your method for /demo/first is not public.
  • The proper way to call this endpoint should be curl localhost:8080/demo/first?name=Biagio -H &quot;Accept: application/json&quot;

答案2

得分: 0

你应该能够通过浏览器验证端点 localhost:8080/demo/first?name=Biagio,因为它默认提交 GET 请求。

我成功复现了这个问题,你应该移除类级别的映射,并在 MainController 的方法中提供完整路径:

  1. @RestController
  2. public class MainController {
  3. @Autowired
  4. private UserRepository userRepository;
  5. @Transactional
  6. @PostMapping("/demo/add")
  7. public @ResponseBody String addNewUser (
  8. @RequestParam String name,
  9. @RequestParam String email,
  10. @RequestParam String surname)
  11. {
  12. User n = new User();
  13. n.setName(name);
  14. n.setSurname(surname);
  15. n.setEmail(email);
  16. userRepository.create(n);
  17. return "Saved";
  18. }
  19. @GetMapping("/demo/first")
  20. public User one(@RequestParam String name) {
  21. System.out.print(name);
  22. return userRepository.findFirstByName(name);
  23. }
  24. }

以下 curl 请求应该成功:

  1. curl -X GET localhost:8080/demo/first?name=Biagio
  2. curl -G localhost:8080/demo/first?name=Biagio
  3. curl -G localhost:8080/demo/first -d name=Biagio

更新
以下修复方法在方法级别的映射中也可以正常工作,无需在映射前添加斜杠:

  1. @Controller
  2. @RequestMapping("/demo") // 尾部斜杠可选,也可以是 "/demo/"
  3. public class MainController {
  4. @Autowired
  5. private UserRepository userRepository;
  6. @Transactional
  7. @PostMapping("add") // 无前导斜杠
  8. public @ResponseBody String addNewUser (/* ... */) {
  9. // ...
  10. }
  11. @GetMapping("first") // 无前导斜杠
  12. public User one(@RequestParam String name) {
  13. return userRepository.findFirstByName(name);
  14. }
  15. }
英文:

You should be able to verify the endpoint localhost:8080/demo/first?name=Biagio using your browser, as it submits GET requests by default.

I managed to reproduce this issue, you should remove the class-level mapping and provide full path on the method in MainController:

  1. @RestController
  2. public class MainController {
  3. @Autowired
  4. private UserRepository userRepository;
  5. @Transactional
  6. @PostMapping(&quot;/demo/add&quot;)
  7. public @ResponseBody String addNewUser (
  8. @RequestParam String name,
  9. @RequestParam String email,
  10. @RequestParam String surname)
  11. {
  12. User n = new User();
  13. n.setName(name);
  14. n.setSurname(surname);
  15. n.setEmail(email);
  16. userRepository.create(n);
  17. return &quot;Saved&quot;;
  18. }
  19. @GetMapping(&quot;/demo/first&quot;)
  20. public User one(@RequestParam String name) {
  21. System.out.print(name);
  22. return userRepository.findFirstByName(name);
  23. }
  24. }

The following curl requests should succeed:

  1. curl -X GET localhost:8080/demo/first?name=Biagio
  2. curl -G localhost:8080/demo/first?name=Biagio
  3. curl -G localhost:8080/demo/first -d name=Biagio

Update<br/>
The following fix is also working without leading slashes in the method-level mappings:

  1. @Controller
  2. @RequestMapping(&quot;/demo&quot;) // trailing slash optional, could be &quot;/demo/&quot;
  3. public class MainController {
  4. @Autowired
  5. private UserRepository userRepository;
  6. @Transactional
  7. @PostMapping(&quot;add&quot;) // no leading slash
  8. public @ResponseBody String addNewUser (/* ... */) {
  9. // ...
  10. }
  11. @GetMapping(&quot;first&quot;) // no leading slash
  12. public User one(@RequestParam String name) {
  13. return userRepository.findFirstByName(name);
  14. }
  15. }

huangapple
  • 本文由 发表于 2020年10月2日 18:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64169880.html
匿名

发表评论

匿名网友

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

确定