Spring-Data项目未能接收我的Get/Post请求,显示“404未找到”。

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

Spring-Data Project is not receiving my Get/Post "404 not found"

问题

  1. I finished creating a simple Spring-boot project in which I can enter users and through the Get command it returns me the name (from a list of identical names) with the oldest entry date. Unfortunately, every time I ask for Get it returns this **ERROR**:
  2. D:\>curl -G localhost:8080/demo/first?=Biagio
  3. {"timestamp":"2020-10-04T22:46:35.996+00:00","status":404,"error":"Not Found","message":"","path":"/demo/first"}
  4. And to each of my POST / Add requests like this **ERROR**:
  5. D:\>curl localhost:8080/demo/add -d name=Giovanni -d email=giovanni@gmail.com -d surname=Jackie
  6. {"timestamp":"2020-10-04T22:40:51.928+00:00","status":404,"error":"Not Found","message":"","path":"/demo/add"}
  7. Below I enter the interested parties of my project to try to get something out of it, because I have been stuck for days now
  8. AccessingDataMysqlApplication.java
  9. package com.example.accessingdatamysql;
  10. import org.springframework.boot.SpringApplication;
  11. import org.springframework.boot.autoconfigure.SpringBootApplication;
  12. import org.springframework.boot.autoconfigure.domain.EntityScan;
  13. import org.springframework.context.annotation.ComponentScan;
  14. @SpringBootApplication
  15. public class AccessingDataMysqlApplication {
  16. public static void main(String[] args) {
  17. SpringApplication.run(AccessingDataMysqlApplication.class, args);
  18. }
  19. }
  20. MainController.java
  21. package com.example.accessingdatamysql.rest;
  22. import javax.persistence.NoResultException;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.http.HttpStatus;
  25. import org.springframework.http.ResponseEntity;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.web.bind.annotation.GetMapping;
  28. import org.springframework.web.bind.annotation.PathVariable;
  29. import org.springframework.web.bind.annotation.PostMapping;
  30. import org.springframework.web.bind.annotation.RequestMapping;
  31. import org.springframework.web.bind.annotation.RequestMethod;
  32. import org.springframework.web.bind.annotation.RestController;
  33. import com.example.accessingdatamysql.model.UserDto;
  34. import com.example.accessingdatamysql.service.UserService;
  35. @RestController
  36. @RequestMapping("/demo")
  37. public class MainController {
  38. @Autowired
  39. private UserService userService;
  40. @Transactional
  41. @PostMapping(path="/demo/add")
  42. public String addNewUser(@PathVariable("name") String name, @PathVariable("email") String email,
  43. @PathVariable("surname") String surname) {
  44. UserDto n = new UserDto();
  45. n.setName(name);
  46. n.setSurname(surname);
  47. n.setEmail(email);
  48. userService.create(n);
  49. return "User Saved in DB";
  50. }
  51. @SuppressWarnings({ "rawtypes", "unchecked" })
  52. @GetMapping("/demo/first")
  53. public ResponseEntity<UserDto> fetchUser(@PathVariable("name") String name) {
  54. System.out.println(name);
  55. try {
  56. UserDto namefound = userService.findFirstByName(name);
  57. System.out.println("Name found");
  58. ResponseEntity<UserDto> user = new ResponseEntity<UserDto>(namefound, HttpStatus.OK);
  59. return user;
  60. } catch(NoResultException ne) {
  61. System.out.println("User not found");
  62. return new ResponseEntity("User not found with name : " + name, HttpStatus.NOT_FOUND);
  63. }
  64. }
  65. }
  66. UserService.java
  67. package com.example.accessingdatamysql.service;
  68. import org.springframework.stereotype.Service;
  69. import com.example.accessingdatamysql.model.UserDto;
  70. @Service
  71. public interface UserService {
  72. UserDto findFirstByName(String name);
  73. void create(UserDto user);
  74. }
  75. UserServiceImpl.java
  76. package com.example.accessingdatamysql.service;
  77. import org.springframework.beans.factory.annotation.Autowired;
  78. import org.springframework.beans.factory.annotation.Qualifier;
  79. import org.springframework.stereotype.Service;
  80. import com.example.accessingdatamysql.model.UserDto;
  81. import com.example.accessingdatamysql.model.UserEntity;
  82. import com.example.accessingdatamysql.repo.UserRepository;
  83. import com.example.accessingdatamysql.util.UserMapper;
  84. @Service
  85. public class UserServiceImpl implements UserService {
  86. @Autowired
  87. private UserRepository userRepository;
  88. @Autowired
  89. UserMapper mapper;
  90. @Override
  91. public UserDto findFirstByName(String name) {
  92. UserEntity entity = userRepository.findFirstByName(name);
  93. return mapper.toDtoMapper(entity);
  94. }
  95. @Override
  96. public void create(UserDto user) {
  97. UserEntity entity = mapper.toEntityMapper(user);
  98. userRepository.create(entity);
  99. }
  100. }
  101. UserMapper.java
  102. package com.example.accessingdatamysql.util;
  103. import org.mapstruct.Mapper;
  104. import com.example.accessingdatamysql.model.UserDto;
  105. import com.example.accessingdatamysql.model.UserEntity;
  106. @Mapper(componentModel = "spring")
  107. public interface UserMapper {
  108. UserEntity toEntityMapper (UserDto user);
  109. UserDto toDtoMapper (UserEntity userEntity);
  110. }
  111. UserRepository.java
  112. package com.example.accessingdatamysql.repo;
  113. import org.springframework.stereotype.Repository;
  114. import com.example.accessingdatamysql.model.UserEntity;
  115. @Repository
  116. public interface UserRepository {
  117. UserEntity findFirstByName(String name);
  118. void create(UserEntity entity);
  119. }
  120. UserRepositoryImpl.java
  121. package com.example.accessingdatamysql.service;
  122. import javax.persistence.EntityManager;
  123. import javax.persistence.TypedQuery;
  124. import javax.persistence.criteria.CriteriaBuilder;
  125. import javax.persistence.criteria.CriteriaQuery;
  126. import javax.persistence.criteria.Root;
  127. import org.springframework.stereotype.Component;
  128. import com.example.accessingdatamysql.model.UserEntity;
  129. import com.example.accessingdatamysql.repo.UserRepository;
  130. @Component
  131. public class UserRepositoryImpl implements UserRepository {
  132. private final EntityManager em;
  133. public UserRepositoryImpl(EntityManager entityManager) {
  134. this.em = entityManager;
  135. }
  136. @Override
  137. public UserEntity findFirstByName(String name) {
  138. CriteriaBuilder builder = em.getCriteriaBuilder();
  139. CriteriaQuery<UserEntity> criteria = builder.createQuery(UserEntity.class);
  140. Root<UserEntity> root = criteria.from(UserEntity.class);
  141. criteria.select(root).where(builder.equal(root.get("name"), name));
  142. criteria.orderBy(builder.asc(root.get("timestamp")));
  143. TypedQuery<UserEntity> query = em.createQuery(criteria).setMaxResults(1);
  144. return query.getSingleResult();
  145. }
  146. @Override
  147. public void create(UserEntity entity) {
  148. em.persist(entity);
  149. }
  150. }
  151. UserDto.java
  152. package com.example.accessingdatamysql.model;
  153. import java.io.Serializable;
  154. import java.sql.Timestamp;
  155. public class UserDto implements Serializable {
  156. private static final long serialVersionUID = -7621330660870602403L;
  157. private String name;
  158. public String getName() {
  159. return name;
  160. }
  161. public void setName(String name) {
  162. this.name = name;
  163. }
  164. public Timestamp getTimestamp() {
  165. return timestamp;
  166. }
  167. public void setTimestamp(Timestamp timestamp) {
  168. this.timestamp = timestamp;
  169. }
  170. public String getEmail() {
  171. return email;
  172. }
  173. public void setEmail(String email) {
  174. this.email = email;
  175. }
  176. public String getSurname() {
  177. return surname;
  178. }
  179. public void setSurname(String surname) {
  180. this.surname = surname;
  181. }
  182. private Timestamp timestamp;
  183. private String email;
  184. private String surname;
  185. }

请注意,代码中可能存在格式问题,因为我无法在纯文本环境中执行格式化。另外,翻译可能会导致代码格式略有不同。如果需要进一步的帮助,请随时提问。

英文:

I finished creating a simple Spring-boot project in which I can enter users and through the Get command it returns me the name (from a list of identical names) with the oldest entry date. Unfortunately, every time I ask for Get it returns this ERROR:

  1. D:\&gt;curl -G localhost:8080/demo/first?=Biagio
  2. {&quot;timestamp&quot;:&quot;2020-10-04T22:46:35.996+00:00&quot;,&quot;status&quot;:404,&quot;error&quot;:&quot;Not Found&quot;,&quot;message&quot;:&quot;&quot;,&quot;path&quot;:&quot;/demo/first&quot;}

And to each of my POST / Add requests like this ERROR:

  1. D:\&gt;curl localhost:8080/demo/add -d name=Giovanni -d email=giovanni@gmail.com -d surname=Jackie
  2. {&quot;timestamp&quot;:&quot;2020-10-04T22:40:51.928+00:00&quot;,&quot;status&quot;:404,&quot;error&quot;:&quot;Not Found&quot;,&quot;message&quot;:&quot;&quot;,&quot;path&quot;:&quot;/demo/add&quot;}

Below I enter the interested parties of my project to try to get something out of it, because I have been stuck for days now

AccessingDataMysqlApplication.java

  1. package com.example.accessingdatamysql;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.domain.EntityScan;
  5. import org.springframework.context.annotation.ComponentScan;
  6. @SpringBootApplication
  7. public class AccessingDataMysqlApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(AccessingDataMysqlApplication.class, args);
  10. }
  11. }

MainController.java

  1. package com.example.accessingdatamysql.rest;
  2. import javax.persistence.NoResultException;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import com.example.accessingdatamysql.model.UserDto;
  14. import com.example.accessingdatamysql.service.UserService;
  15. @RestController
  16. @RequestMapping(&quot;/demo&quot;)
  17. public class MainController {
  18. @Autowired
  19. private UserService userService;
  20. @Transactional
  21. //@RequestMapping(value = &quot;/add/&quot;, method = RequestMethod.POST)
  22. @PostMapping(path=&quot;/demo/add&quot;)
  23. public String addNewUser(@PathVariable(&quot;name&quot;) String name, @PathVariable(&quot;email&quot;) String email,
  24. @PathVariable(&quot;surname&quot;) String surname) {
  25. UserDto n = new UserDto();
  26. n.setName(name);
  27. n.setSurname(surname);
  28. n.setEmail(email);
  29. userService.create(n);
  30. return &quot;User Saved in DB&quot;;
  31. }
  32. @SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
  33. //@RequestMapping(value = &quot;/fetchUser/{name}&quot;, method = RequestMethod.GET)
  34. @GetMapping(&quot;/demo/first&quot;)
  35. public ResponseEntity&lt;UserDto&gt; fetchUser(@PathVariable(&quot;name&quot;) String name) {
  36. System.out.println(name);
  37. try {
  38. UserDto namefound = userService.findFirstByName(name);
  39. System.out.println(&quot;Name found&quot;);
  40. ResponseEntity&lt;UserDto&gt; user = new ResponseEntity&lt;UserDto&gt;(namefound, HttpStatus.OK);
  41. return user;
  42. } catch(NoResultException ne) {
  43. System.out.println(&quot;User not found&quot;);
  44. return new ResponseEntity(&quot;User not found with name : &quot; + name, HttpStatus.NOT_FOUND);
  45. }
  46. }
  47. }

UserService.java

  1. package com.example.accessingdatamysql.service;
  2. import org.springframework.stereotype.Service;
  3. import com.example.accessingdatamysql.model.UserDto;
  4. @Service
  5. public interface UserService {
  6. UserDto findFirstByName(String name);
  7. void create(UserDto user);
  8. }

UserServiceImpl.java

  1. package com.example.accessingdatamysql.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.stereotype.Service;
  5. import com.example.accessingdatamysql.model.UserDto;
  6. import com.example.accessingdatamysql.model.UserEntity;
  7. import com.example.accessingdatamysql.repo.UserRepository;
  8. import com.example.accessingdatamysql.util.UserMapper;
  9. @Service
  10. public class UserServiceImpl implements UserService {
  11. @Autowired
  12. private UserRepository userRepository;
  13. @Autowired
  14. UserMapper mapper;
  15. @Override
  16. public UserDto findFirstByName(String name) {
  17. UserEntity entity = userRepository.findFirstByName(name);
  18. return mapper.toDtoMapper(entity);
  19. }
  20. @Override
  21. public void create(UserDto user) {
  22. UserEntity entity = mapper.toEntityMapper(user);
  23. userRepository.create(entity);
  24. }
  25. }

UserMapper.java

  1. package com.example.accessingdatamysql.util;
  2. import org.mapstruct.Mapper;
  3. import com.example.accessingdatamysql.model.UserDto;
  4. import com.example.accessingdatamysql.model.UserEntity;
  5. @Mapper(componentModel = &quot;spring&quot;)
  6. public interface UserMapper {
  7. public UserEntity toEntityMapper (UserDto user);
  8. public UserDto toDtoMapper (UserEntity userEntity);
  9. }

UserRepository.java

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

UserRepositoryImpl.java

  1. package com.example.accessingdatamysql.service;
  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. import com.example.accessingdatamysql.model.UserEntity;
  9. import com.example.accessingdatamysql.repo.UserRepository;
  10. @Component
  11. public class UserRepositoryImpl implements UserRepository {
  12. private final EntityManager em;
  13. public UserRepositoryImpl(EntityManager entityManager) {
  14. this.em = entityManager;
  15. }
  16. @Override
  17. public UserEntity findFirstByName(String name) {
  18. CriteriaBuilder builder = em.getCriteriaBuilder();
  19. CriteriaQuery&lt;UserEntity&gt; criteria = builder.createQuery(UserEntity.class);
  20. Root&lt;UserEntity&gt; root = criteria.from(UserEntity.class);
  21. criteria.select(root).where(builder.equal(root.get(&quot;name&quot;), name));
  22. criteria.orderBy(builder.asc(root.get(&quot;timestamp&quot;)));
  23. TypedQuery&lt;UserEntity&gt; query = em.createQuery(criteria).setMaxResults(1);
  24. return query.getSingleResult();
  25. }
  26. @Override
  27. // per la creazione//
  28. public void create(UserEntity entity) {
  29. em.persist(entity);
  30. }
  31. }

UserDto.java

  1. package com.example.accessingdatamysql.model;
  2. import java.io.Serializable;
  3. import java.sql.Timestamp;
  4. public class UserDto implements Serializable {
  5. /**
  6. *
  7. */
  8. private static final long serialVersionUID = -7621330660870602403L;
  9. /**
  10. *
  11. */
  12. private String name;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Timestamp getTimestamp() {
  20. return timestamp;
  21. }
  22. public void setTimestamp(Timestamp timestamp) {
  23. this.timestamp = timestamp;
  24. }
  25. public String getEmail() {
  26. return email;
  27. }
  28. public void setEmail(String email) {
  29. this.email = email;
  30. }
  31. public String getSurname() {
  32. return surname;
  33. }
  34. public void setSurname(String surname) {
  35. this.surname = surname;
  36. }
  37. private Timestamp timestamp;
  38. private String email;
  39. private String surname;
  40. }

If you need I can also insert User.java and the pom, but the pom has no problems as the dependencies are all correct.

答案1

得分: 4

你在GET和POST方法的路径描述中多了一个demo,你应该把它移除:

  1. @GetMapping("/demo/first")
  2. @PostMapping(path = "/demo/first")

应该改为:

  1. @GetMapping("/first")
  2. @PostMapping(path = "/first")

这是因为你在类级别的RequestMapping注解中已经定义了demo

英文:

You have an additional demo in your path descriptions for GET and POST methods, you should remove it:

  1. @GetMapping(&quot;/demo/first&quot;)
  2. @PostMapping(path = &quot;/demo/first&quot;)

It should be :

  1. @GetMapping(&quot;/first&quot;)
  2. @PostMapping(path = &quot;/first&quot;)

This because you have already defined demo in the RequestMapping annotation in the class level.

huangapple
  • 本文由 发表于 2020年10月5日 06:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200645.html
匿名

发表评论

匿名网友

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

确定