Spring Boot不识别MongoDB RestController类。

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

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(&quot;/v2&quot;)
public class UserController {
	
	@Autowired
	private UserRepository userRepository;

	@PostMapping(&quot;/AddUser&quot;)
	public String saveUser(@RequestBody User user) {
		userRepository.save(user);
		return &quot;Added user with id: &quot; + user.getId();
	}

    @GetMapping(&quot;/all&quot;)
    public List&lt;User&gt; getAll(){
        List&lt;User&gt; 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

  1. SpringBootMain中删除@Bean ResourceConfig resourceConfig() {...}。现在,您的API /v2 将按预期工作。

  2. 您的API /v1 将不起作用,因为它使用其他库。您需要将@Path更改为@GetMapping@PostMapping,并将@RestController添加到您的Version1Api类中。

解决方案#2

您忽略了Spring-Boot本机Rest API,并实现了Glassfish服务器。

  1. 添加UserController.class 引用
@Bean 
ResourceConfig resourceConfig() { 
    return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class, **UserController.class**); 
}
  1. 对于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

  1. From SpringBootMain remove @Bean ResourceConfig resourceConfig() {...}. Now, you API /v2 will work as expected.

  2. Your API /v1 won't work because it uses other library. You need to change @Path to @GetMapping or @PostMapping and add @RestController into your Version1Api class.

Solution #2

You ignore Spring-Boot native Rest API and implement Glassfish Server.

  1. Add UserController.class reference

>@Bean ResourceConfig resourceConfig() {
return new ResourceConfig().registerClasses(Version1Api.class, Login.class, SignUp.class, Paypal.class, UserController.class); }

  1. For UserController change @RestController to @Path(&quot;/v2&quot;)

@Path(&quot;/v2&quot;)
public class UserController {
	
	@Autowired
	private UserRepository userRepository;

	@POST
	@Path(&quot;/AddUser&quot;)
	@Produces(MediaType.TEXT_PLAIN)
	public String saveUser(@RequestBody User user) {
		userRepository.save(user);
		return &quot;Added user with id: &quot; + user.getId();
	}

	@GET
	@Path(&quot;/all&quot;)
	@Produces(MediaType.APPLICATION_JSON)
    public List&lt;User&gt; getAll(){
        List&lt;User&gt; users = this.userRepository.findAll();
        return users;
    }
}

Now both API will work as expected

huangapple
  • 本文由 发表于 2020年3月4日 04:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/60514679.html
匿名

发表评论

匿名网友

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

确定