1

Пытаюсь удалить категорию. Код серверной части (сомневаюсь что вызывается вообще он, потому что запрос именно по такому адресу не работает) :

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class BookCategoryController {
 @DeleteMapping("/categories/{id}")
    public ResponseEntity<HttpStatus> deleteBookCategory(@PathVariable("id") long id) {
        try {
            bookCategoryRepository.deleteById(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
    }

Клиентская часть:

    const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'my-auth-token'
  })
};
@Injectable({
  providedIn: 'root'
})
export class CategoryService {
  private categoryUrl = 'http://localhost:8081/api/category';
  constructor(private httpClient: HttpClient) { }
  delete(id:number): Observable<{}>{
    return this.httpClient.delete(`${this.categoryUrl}/${id}`, httpOptions);
  }
}

Если надо то вот еще в вебконфиге

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests().antMatchers("/api/auth/**").permitAll()
                .antMatchers("/api/books/**", "/api/departments/**", "/api/universities/**",
                        "/api/faculties/**", "/api/users/**", "/api/roles/**", "/api/authors/**",
                        "/api/upload/**", "/api/files/**", "/api/category/**","/api/users/rolesAdd/**").permitAll()
                .anyRequest().authenticated();
        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
            }

А это в репозиториконфиг

@Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {


        config.exposeIdsFor(entityManager.getMetamodel().getEntities().stream()
                .map(Type::getJavaType)
                .toArray(Class[]::new));
        config.getCorsRegistry()
                .addMapping("/**")
                .allowedOrigins("http://localhost:4200");
    }
2
  • ПОпробуйте добавить .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS") в config.getCorsRegistry()
    – ЮрийСПб
    21 мая 2020 в 21:09
  • Помогло! Спасибо 26 мая 2020 в 16:34

1 ответ 1

0

Попробуйте добавить

.allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS")

в config.getCorsRegistry()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.