【发布时间】:2019-11-13 02:09:35
【问题描述】:
我有这个映射:
User 1----------------------------* Expertises
我用的是控制器SpringBoot,我的控制器是
@RestController
@CrossOrigin(origins = "http://localhost:4200", "http://localhost:6227")
@RequestMapping("/api/auth")
public class UserController
{
@PostMapping("/signup/{expertises}")
public ResponseEntity<String> registerUser(@Valid @RequestBody SignUpForm signUpRequest, @PathVariable List<String> expertises)
{
}
}
我将注解 @CrossOrigin 添加到所有存储库中
@CrossOrigin(origins = {"http://localhost:4200", "http://localhost:6227"}, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE }, maxAge = 3600)
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
主要类是:
@SpringBootApplication
public class SpringBootJwtAuthenticationApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJwtAuthenticationApplication.class, args);
}
@Bean
public WebMvcConfigurer configurer()
{
return new WebMvcConfigurer()
{
@Override
public void addCorsMappings(CorsRegistry registry)
{
registry.addMapping("/*")
.allowedOrigins("http://localhost:4200", "http://localhost:6227");
}
};
}
}
我添加了文件 MyConfiguration(正如 Ananthapadmanabhan 先生建议的那样)
前端(Angular6)
所以我想使用这种方法向一位用户添加专业知识列表:
onSubmit()
{
this.submitted = true;
console.log('---------SelectedExpertise:' + this.selectedExpertiseCheckBox);
this.userService.signUpUser(this.user,
this.selectedExpertiseCheckBox)
.subscribe(data => console.log("---------------Create user:" + data)
,error => console.log(error));
this.user = new User();
}
在哪里
signUpUser(value: any, listExp: String[]): Observable<Object>
{
return this.http.post(`${this.baseUrl}/signup/${listExp}`, value);
}
您对解决这个问题有什么想法吗? 谢谢。
【问题讨论】:
-
你能提供你的spring boot版本吗?以及来自 chrome 的 OPTION 调用的屏幕截图。
-
您好 @YogeshPrajapati 先生,感谢您的回复,
<modelVersion>4.0.0</modelVersion>
标签: spring-boot angular6