请求Spring Controller返回404未找到。

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

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:

请求Spring Controller返回404未找到。

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(&quot;/**&quot;).allowedOrigins(&quot;http://localhost:4200&quot;);
		}
	};
}
}

TaskRepository:

@Path(&quot;tasks&quot;)
@ApiIgnore
@Component
@AllArgsConstructor
public class TaskResource {

private final TaskService taskService;

@GET
@Produces(APPLICATION_JSON)
public List&lt;Task&gt; getAllTasks() {
    return taskService.getTasks();
}

TaskService:

@Service
@RequiredArgsConstructor
public class TaskService {

private final TaskRepository taskRepository;

public List&lt;Task&gt; getTasks() {
    return taskRepository.findAll();
}

Project Structure:

请求Spring Controller返回404未找到。

答案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 &quot;org.springframework.boot:spring-boot-starter-jersey&quot;

pom.xml

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-jersey&lt;/artifactId&gt;
&lt;/dependency&gt;

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 = &quot;tasks&quot;)
public class TaskResource {

    @GetMapping(path = &quot;&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
    public List&lt;String&gt; getAllTasks() {
        return Arrays.asList(&quot;a&quot;,&quot;b&quot;);
    }
}

Also you will need to remove JAX-RS from spring to use this Spring MVC annoatations to work.

huangapple
  • 本文由 发表于 2020年10月25日 01:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64516021.html
匿名

发表评论

匿名网友

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

确定