英文:
Can not access endpoint URL in Springboot Rest web project
问题
我已在eclipse中初始化了一个SpringBoot Rest项目,并将其设置为动态Web项目。遵循了3层原则,并在控制器类中声明了端点URL。项目部署正常,但当我尝试访问返回404错误的端点时出现问题。请参考下面的示例。
使用的编译器 - Maven,服务器 - apache tomcat 9.0
MainClass.java
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
@Configuration
public class PmsAdvanced extends SpringBootServletInitializer implements
WebMvcConfigurer{
public static void main(String[] args) {
SpringApplication.run(PmsAdvanced.class, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index.jsp");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
示例控制器类
@RestController
@EnableWebMvc
@RequestMapping("/test")
public class PmsAdvancedController implements WebMvcConfigurer {
@PostMapping("/PMS")
public String sayHello(@RequestParam(value = "myName", defaultValue = "divi") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/PMSA")
public String sayAge(@RequestParam(value = "age", defaultValue = "100") String age) {
return String.format("I'm %s!", age);
}
}
我一直在尝试访问的URL - http://localhost:8081/pms_advance_be/test/PMSA
英文:
I've Initialized a SpringBoot Rest in eclipse and made it a Dynamic Web project. A 3-tire principle has been followed and endpoint URLs declared in controller classes. The project deploys fine but once I try to access an endpoint that returns a 404-error. Please refer to the example below.
Used Compiler - Maven and Server - apache tomcat 9.0
MainClass.java
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
@Configuration
public class PmsAdvanced extends SpringBootServletInitializer implements
WebMvcConfigurer{
public static void main(String[] args) {
SpringApplication.run(PmsAdvanced.class, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index.jsp");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
Sample Controller class
@RestController
@EnableWebMvc
@RequestMapping("/test")
public class PmsAdvancedController implements WebMvcConfigurer {
@PostMapping("/PMS")
public String sayHello(@RequestParam(value = "myName", defaultValue = "divi") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/PMSA")
public String sayAge(@RequestParam(value = "age", defaultValue = "100") String age) {
return String.format("I'm %s!", age);
}}
URL I've been trying to access - http://localhost:8081/pms_advance_be/test/PMSA
答案1
得分: 2
如果你将某个东西注释为 @Configuration
,那么你在告诉 Spring 这个类是 @Beans
的来源。因此,你不应该在同一个类中使用 @RequestMapping
/@RestController
/@GetMapping
。将这些项目的相关部分分离到另一个类中。
我也相当确定,对于你的设置,你不需要重复添加 @EnableAutoConfiguration
,可能只需要将它(如果需要的话)与根的 @SpringBootApplication
注解一起使用(可能还可以与任何 @SpringBootTest
注解一起使用,但请不要引用我关于这点的话)。如果你将它们移除并阅读返回的任何异常信息,你应该能够更好地使这个工作起来。
在 MVC 风格的应用程序中(不论使用哪种语言),典型的命名约定是所有请求由名为 "BlahBlah...Controller" 的类处理,用于处理传入的 HTTP 请求。
因此,总结一下,为了使这个工作起来,我会从以下几点开始:
- 移除所有以
@EnableAutoConfiguration
开头/相等的行。 - 将
PmsAdvancedController
重命名为PmsAdvancedConfiguration
,并且仅保留其中的addViewControllers
方法。 - 从
PmsAdvancedConfiguration
中移除@RestController
和@RequestMapping("/test")
。 - 创建一个名为
PmsAdvanceController
的新类,并在其中添加@RequestMapping("/test")
注解,以及标记有@GetMapping
和@PostMapping
的方法。 @RestController
结合了@Controller
和@ResponseBody
,它并不是必需的。删除它。- 我无法看出你在
WebMvcConfigurer
中尝试做什么,但我猜测你可能在根据某种示例进行操作,从你的描述中我能感受到 "Hello Worldiness"… 查看其他在线示例和文档,以下两个注解应该就足够了:@EnableWebMvc
和@Configuration
,所有其他注解都可以移除。
编辑:
如果你将以下内容放入一个单独的 Java 文件中并运行 main
方法,它会正常工作:
(以下是代码部分,已被保留。)
测试:
curl -X GET http://localhost:8080/test/PMSA?age=30
或在浏览器中访问:http://localhost:8080/test/PMSA?age=30
输出:
I'm 30!
注意:实际上我并不是30岁。
英文:
If you annotate something as @Configuration
then you are telling spring that the class is a source of @Beans
. You should therefore not have @RequestMapping
/@RestController
/@GetMapping
inside the same class. Separate the relevant parts for those items out into another class.
I'm also pretty sure for the set up you have you don't need to repeatedly add @EnableAutoConfiguration
and probably only need it (if at all) alongside the root @SpringBootApplication
annotation (possibly alongside any @SpringBootTest
as well but don't quote me on this). If you get rid of them and read any exceptions you get back, you should be in a better position to get this working.
In MVC style apps (regardless of language) the typical naming convention is that all requests are handled by classes named "BlahBlah...Controller", handle incoming HTTP requests.
So in summary, to get this working, I'd start with:
- remove all lines starting/equal to
@EnableAutoConfiguration
- rename
PmsAdvancedController
toPmsAdvancedConfiguration
and keep methodaddViewControllers
within it only. - remove
@RestController
&@RequestMapping("/test")
fromPmsAdvancedConfiguration
- create a new class
PmsAdvanceController
and add to it the annotations@RequestMapping("/test")
methods marked@GetMapping
and@PostMapping
@RestController
combines@Controller
and@ResponseBody
- it is not necessary. Delete.- I can't see what you're attempting to do with the
WebMvcConfigurer
but I am assuming you're following some sort of example judging by the "Hello Worldiness" of it... Looking at other examples online and the documentation it should work with the following two annotations:@EnableWebMvc
and@Configuration
all other annotations can be removed.
EDIT:
If you drop the below into a single Java file and run the main
method it works:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
public class PmsAdvanced extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(PmsAdvanced.class, args);
}
}
@Configuration
class PmsAdvancedConfiguration implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index.jsp");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
@RestController
@RequestMapping("/test")
class PmsAdvancedController {
@PostMapping("/PMS")
public String sayHello(@RequestParam(value = "myName", defaultValue = "divi") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/PMSA")
public String sayAge(@RequestParam(value = "age", defaultValue = "100") String age) {
return String.format("I'm %s!", age);
}
}
TESTING:
curl -X GET http://localhost:8080/test/PMSA?age=30
Or visit in browser: http://localhost:8080/test/PMSA?age=30
Output:
I'm 30!
Note: I'm not actually 30.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论