我有以下域对象并定义了DTO。
Country.java
@Data
@Entity
public class Country extends ResourceSupport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long countryID;
@NotBlank(message = "Country name is a required field")
private String countryName;
private String countryNationality;
}CountryDTO.java
@Data
public class CountryDTO {
private List<Country> countries;
}我已经重写了国家类的RepositoryRestController中的POST方法。
@RepositoryRestController
public class CountryController {
@Autowired
private CountryRepository repo;
@RequestMapping(method = POST, value = "countries")
public @ResponseBody ResponseEntity<?> createCountry(@RequestBody Resource<CountryDTO> dto,
Pageable page, PersistentEntityResourceAssembler resourceAssembler) {
Country savedCountry = repo.save(dto.getContent().getCountries());
return new ResponseEntity<>(resourceAssembler.toResource(savedCountry), HttpStatus.OK);
}
}现在,我已经定义了一个RepositoryEventHandler来处理验证。
@Component
@RepositoryEventHandler
public class CountryHandler {
@HandleBeforeCreate
public void handleBeforeCreate(Country country) {
System.out.println("testing");
}但是,当我向端点http://localhost:8080/countries发送POST请求时,就不会调用均衡器。我做错什么了吗?
更新1:我使用Postman向端点发送以下JSON。
"countries":[{
"countryName":"Australia",
"countryNationality":"Australian"
}]发布于 2017-06-05 12:41:48
在不知道如何调用请求的情况下,很难给出确切的解决方案。但可能的原因是您缺少斜杠符号@RequestMapping值属性:
@RequestMapping(method = POST, value = "countries")应:
@RequestMapping(method = POST, value = "/countries")发布于 2017-06-05 12:50:52
在AppConfigration中将Bean定义为
@Configuration
@EnableAsync
public class AppConfig {
@Bean
CountryHandler countryHandler (){
return new CountryHandler ();
}
}那就行了。
发布于 2017-06-05 14:26:48
尝试从以下位置编辑Controller类注释:
@RepositoryRestController至
@RestController方法注释主要来自:
@RequestMapping(method = POST, value = "countries")至
@RequestMapping(value = "/countries", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)PS:如果您要返回json,请使用produces = MediaType.APPLICATION_JSON_VALUE。
https://stackoverflow.com/questions/44369093
复制相似问题