英文:
Request to Spring Controller returns 404 not found
问题
以下是您提供的内容的翻译:
我正在尝试设置一个Spring MVC应用程序,但每次我从Postman调用http://localhost:9001/tasks API时,我都会收到以下错误:
(图片)
以下是我的代码:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class TaskManagerApplication {
public static void main(String[] args) {
SpringApplication.run(TaskManagerApplication.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:4200");
}
};
}
}
TaskRepository:
@Path("tasks")
@ApiIgnore
@Component
@AllArgsConstructor
public class TaskResource {
private final TaskService taskService;
@GET
@Produces(APPLICATION_JSON)
public List<Task> getAllTasks() {
return taskService.getTasks();
}
}
TaskService:
@Service
@RequiredArgsConstructor
public class TaskService {
private final TaskRepository taskRepository;
public List<Task> getTasks() {
return taskRepository.findAll();
}
}
项目结构:
(图片)
英文:
I am trying to set up a Spring MVC app but every time I call the http://localhost:9001/tasks API from postman I get the following error:
Here is my code:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class TaskManagerApplication {
public static void main(String[] args) {
SpringApplication.run(TaskManagerApplication.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:4200");
}
};
}
}
TaskRepository:
@Path("tasks")
@ApiIgnore
@Component
@AllArgsConstructor
public class TaskResource {
private final TaskService taskService;
@GET
@Produces(APPLICATION_JSON)
public List<Task> getAllTasks() {
return taskService.getTasks();
}
TaskService:
@Service
@RequiredArgsConstructor
public class TaskService {
private final TaskRepository taskRepository;
public List<Task> getTasks() {
return taskRepository.findAll();
}
Project Structure:
答案1
得分: 0
你正在Spring Boot中使用JAX-RS
。Spring以其自己的方式处理REST,如果你想使用JAX-RS
而不是Spring的REST注解,你需要进行一些额外的配置。
首先,在你的build.gradle
或者pom.xml
文件中添加一个JAX-RS
依赖。我猜你可能已经做过了。Jersey是JAX-RS
的一个实现,如果你想要添加它,需要进行以下步骤。
build.gradle
implementation "org.springframework.boot:spring-boot-starter-jersey"
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
之后,你需要在Spring中注册JAX-RS
的端点。我猜你可能漏掉了这一步。
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JaxrsConfig extends ResourceConfig {
public JaxrsConfig() {
register(TaskResource.class);
}
}
完成这些步骤后,你的JAX-RS
端点将会在Spring中注册。
但我建议你如果在使用Spring的话,还是遵循Spring的注解方式。如果你使用Spring的注解,你的代码将会像这样:
@RestController
@RequestMapping(path = "tasks")
public class TaskResource {
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
public List<String> getAllTasks() {
return Arrays.asList("a", "b");
}
}
另外,如果要使用Spring MVC注解,你还需要将JAX-RS从Spring中移除。
英文:
You are using JAX-RS
in spring boot. Spring handles rest in its own way, if you want to use JAX-RS
instead of Springs Rest Annotations, you need to do some extra configurations.
First, you need to add a JAX-RS
dependency in your build.gradle
or pom.xml
file. I guess you have already done that. Jersey is one of the JAX-RS
implementation, if you want to add this, you need to do the following.
build.gradle
implementation "org.springframework.boot:spring-boot-starter-jersey"
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
After that, you need to register the JAX-RS
endpoints with Spring. I guess you missed this step.
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JaxrsConfig extends ResourceConfig {
public JaxrsConfig() {
register(TaskResource.class);
}
}
After this, your JAX-RS
endpoints will be registered with spring.
But I will suggest you to follow spring annotations if you are using spring. If you use spring annotations your code will look like this.
@RestController
@RequestMapping(path = "tasks")
public class TaskResource {
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
public List<String> getAllTasks() {
return Arrays.asList("a","b");
}
}
Also you will need to remove JAX-RS from spring to use this Spring MVC annoatations to work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论