英文:
Spring Boot does not recognize MongoDB RestController class
问题
以下是您要翻译的内容:
我对MongoDB还不熟悉,正在尝试与我的SpringBoot应用程序一起使用它。我已经按照在线教程进行了操作,并下载了他们的代码并成功运行了它。
然而,出于某种原因,我的项目无法打印出以下内容:
RequestMappingHandlerMapping:映射“{[/findAllBooks/{id}],methods=[GET]}”
我想知道是否有人能够指导我,是否是由于我的项目结构的性质所致。
我不确定我的SpringBootMain是否能够看到我的控制器类。
我的项目结构在这里查看最佳:
https://github.com/emuldrew855/backend/tree/A/B-Testing/src/main/java/com/ebay/queens/demo
我的控制器类
package com.ebay.queens.demo.resource;
@RestController
@RequestMapping("/v2")
public class UserController {
@Autowired
private UserRepository userRepository;
@PostMapping("/AddUser")
public String saveUser(@RequestBody User user) {
userRepository.save(user);
return "Added user with id: " + user.getId();
}
@GetMapping("/all")
public List<User> getAll(){
List<User> users = this.userRepository.findAll();
return users;
}
}
我的主类
package com.ebay.queens.demo;
@SpringBootConfiguration
@SpringBootApplication
public class SpringBootMain implements CommandLineRunner {
@Autowired
private TokenUtilityClass tokenUtilityClass;
@Bean
ResourceConfig resourceConfig() {
return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class);
}
@Override
public void run(String... args) throws Exception {
// test.authenticationToken();
}
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
英文:
I am new to MongoDB and I am trying to use it with my SpringBoot application. I have followed my tutorials online and have downloaded their code and got it execute.
However for whatever reason my project fails to be able to print out
RequestMappingHandlerMapping : Mapped “{[/findAllBooks/{id}],methods=[GET]}”
I was wondering if anyone would be able to advise me if it is due to the nature of my project structure .
I wasn’t sure if my SpringBootMain could see my Controller class.
My project structure is best viewed here
https://github.com/emuldrew855/backend/tree/A/B-Testing/src/main/java/com/ebay/queens/demo
My Controller class
package com.ebay.queens.demo.resource;
@RestController
@RequestMapping("/v2")
public class UserController {
@Autowired
private UserRepository userRepository;
@PostMapping("/AddUser")
public String saveUser(@RequestBody User user) {
userRepository.save(user);
return "Added user with id: " + user.getId();
}
@GetMapping("/all")
public List<User> getAll(){
List<User> users = this.userRepository.findAll();
return users;
}
}
My main class
package com.ebay.queens.demo;
@SpringBootConfiguration
@SpringBootApplication
public class SpringBootMain implements CommandLineRunner {
@Autowired
private TokenUtilityClass tokenUtilityClass;
@Bean ResourceConfig resourceConfig() {
return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class); }
@Override
public void run(String... args) throws Exception {
// test.authenticationToken();
}
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
答案1
得分: 1
我已经找出为什么不起作用了...您正在使用两个不兼容的Web服务API...
Spring-Boot具有用于使用@RestController
注释与API Rest一起工作的本机API。您不需要使用Glassfish
服务器。
解决方案#1
-
从
SpringBootMain
中删除@Bean ResourceConfig resourceConfig() {...}
。现在,您的API/v2
将按预期工作。 -
您的API
/v1
将不起作用,因为它使用其他库。您需要将@Path
更改为@GetMapping
或@PostMapping
,并将@RestController
添加到您的Version1Api
类中。
解决方案#2
您忽略了Spring-Boot本机Rest API,并实现了Glassfish
服务器。
- 添加
UserController.class
引用
@Bean
ResourceConfig resourceConfig() {
return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class, **UserController.class**);
}
- 对于
UserController
,将@RestController
更改为@Path("/v2")
@Path("/v2")
public class UserController {
@Autowired
private UserRepository userRepository;
@POST
@Path("/AddUser")
@Produces(MediaType.TEXT_PLAIN)
public String saveUser(@RequestBody User user) {
userRepository.save(user);
return "Added user with id: " + user.getId();
}
@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getAll(){
List<User> users = this.userRepository.findAll();
return users;
}
}
现在两个API将按预期工作。
英文:
I've figured out why is not working... You are using 2 different WebService API which are incompatible...
Spring-Boot has native API to work with API Rest with @RestController
annotation. You don't need to use Glassfish
server.
Solution #1
-
From
SpringBootMain
remove@Bean ResourceConfig resourceConfig() {...}
. Now, you API/v2
will work as expected. -
Your API
/v1
won't work because it uses other library. You need to change@Path
to@GetMapping
or@PostMapping
and add@RestController
into yourVersion1Api
class.
Solution #2
You ignore Spring-Boot native Rest API and implement Glassfish
Server.
- Add
UserController.class
reference
>@Bean ResourceConfig resourceConfig() {
return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class, UserController.class); }
- For
UserController
change@RestController
to@Path("/v2")
@Path("/v2")
public class UserController {
@Autowired
private UserRepository userRepository;
@POST
@Path("/AddUser")
@Produces(MediaType.TEXT_PLAIN)
public String saveUser(@RequestBody User user) {
userRepository.save(user);
return "Added user with id: " + user.getId();
}
@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getAll(){
List<User> users = this.userRepository.findAll();
return users;
}
}
Now both API will work as expected
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论