我有一个工作中的api,将api数据发送到后端并将其发布到DB。 当我向表单添加输入文件类型并将其附加到数据表单时,出现错误。
我总是有这个错误:
“错误:”内部服务器错误“
消息:“无法识别的字段“文件”(类org.sid.model.User),未标记为可忽略(6个已知属性:“文件名”,“标识”,“电子邮件”,“名称”,“密码”,“徽标” “])↵在[来源:(字串)” {“名称”:“ ss”,“电子邮件”:“ sss”,“密码”:“ sssssssss”,“文件”:“ C:\ fakepath \ ooredoo_758118.png “}”;第1行,第58列(通过参考链:org.sid.model.User [“ file”])“ 路径:“ /用户” “
控制器
@CrossOrigin(origins = { "http://localhost:4200", "http://localhost:9000" }, maxAge = 36000)
/*@RequestMapping(value="/users" , method=RequestMethod.POST)*/
@RequestMapping(value="/users" , headers = ("content-type=multipart/*"), method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE ,
produces = { "application/json" }
)
public User save(@RequestPart("file") MultipartFile f ,
@RequestPart ("user") String u)
throws JsonParseException, JsonMappingException, IOException{
User user = new ObjectMapper().readValue(u, User.class) ;
user.setLogo(f.getBytes());
return userRepository.save(user);
}
User.java
@Entity
public class User {
@Id @GeneratedValue
private Long id ;
private String name ;
private String email ;
private String password ;
@Column(name="logo")
private byte[] logo;
@Column(name="file_name")
private String fileName ;
}
mainApplication.java
@SpringBootApplication
public class MyChebbaApplication {
public static void main(String[] args) {
SpringApplication.run(MyChebbaApplication.class, args);
}
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("10240KB");
factory.setMaxRequestSize("10240KB");
return factory.createMultipartConfig();
}
}
Clien Side
component.html
<div class="container" >
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" #form="ngForm" (ngSubmit)="onSubmit(form)">
<div class="form-group">
<label for="name" class="control-label col-md-4">Nom Complet</label>
<div class="col-md-6">
<input id="name" type="text" name="name" #userName="ngModel" ngModel class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="email" class="control-label col-md-4">E-mail Address</label>
<div class="col-md-6">
<input id="email" type="email" name="email" #userEmail="ngModel" ngModel class="form-control" required>
</div>
</div>
<!-- <div class="form-group">
<label for="phone" class="control-label col-md-4">Phone number</label>
<div class="col-md-6">
<input id="phone" type="phone" name="phone" #phone="ngModel" ngModel class="form-control" >
</div>
</div> -->
<div class="form-group">
<label for="password" class="control-label col-md-4">Password</label>
<div class="col-md-6">
<input id="password" type="text" name="password" #password="ngModel" ngModel class="form-control" required>
</div>
</div>
<div class="form-group">
<label for="file" class="control-label col-md-4">File Upload</label>
<div class="col-md-6">
<input
type="file"
name="file"
accept="image/*"
(change)="onFileSelected($event)"
ngModel class="form-control" >
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary"
[disabled]="form.invalid">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
component.ts
selectedFile: File = null ;
constructor(private userService: UserService) { }
ngOnInit() {
}
onFileSelected(event) {
console.log(event) ;
this.selectedFile = event.target.files[0] ;
}
onSubmit(form: any) {
let fd = new FormData() ;
const user = form.value ;
fd.append('user' , JSON.stringify(user)) ;
fd.append('file' , this.selectedFile ) ;
console.log(fd) ;
this.userService.register(fd )
.then((resp) => {
this.userService.logUserIn(resp) ;
}) ;
}
user.service.ts
register(fd: FormData ): Promise<UserData> {
return this.http.post(`${CONFIG.API_URL}/users`, fd)
.toPromise()
.then((response) => {
let token = response.json().token ;
let user = response.json() ;
let userData = new UserData(token, user) ;
this.router.navigate(['/users']);
return userData ;
});
}
答案 0 :(得分:0)
这意味着您的JSON字符串u不包含“文件”字段。为了忽略缺少的字段,请使用以下命令:
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.readValue(u, User.class);
...
祝你好运。
答案 1 :(得分:0)
这意味着用户没有属性文件。
要解决此问题,只需在 applicatgion.properties 中禁用该配置。
pring.jackson.deserialization.fail-on-unknown-properties=true