java - 春季启动中的“不支持的媒体类型” - Windows
问题描述
我有这样的User
课:
@Data
@Entity
public class User {
@Id
@GeneratedValue
Long userID;
String eMail;
String passwordHash;
//ArrayList<ClassRoom>adminOf=new ArrayList<>();
User() {}
public User(String eMail, String passwordHash) {
this.eMail = eMail;
this.passwordHash = passwordHash;
}
}
在LoadDatabase
我的课堂上:
@Bean
CommandLineRunner initDatabase(UserRepository userRepository) {
return args -> {
log.info("Preloading " + userRepository.save(new User("admin@admin.com", "asdasd")));
log.info("Preloading " + userRepository.save(new User("admin@admin.com", "12345")));
};
}
这给了我这个:
现在,当我发出curl -v localhost:8080/user
这个命令时,它给了我这个:
这是非常正确的,虽然它给了我email
而不是eMail
.
但是当我给
curl -X PUT localhost:8080/user/3 -H 'Content-type:application/json' -d '{"passwordHash":"12345","email":"admin1@admin.com"}'
它说 :
这是非常可怕的。我正在关注本教程。
这是我的UserController
课:
package com.mua.cse616.Controller;
import com.mua.cse616.Model.User;
import com.mua.cse616.Model.UserNotFoundException;
import org.springframework.web.bind.annotation .*;
import java.util.List;
@RestController
class UserController {
private final UserRepository repository;
UserController(UserRepository repository) {
this.repository = repository;
}
// Aggregate root
@GetMapping("/user")
List<User> all() {
return repository.findAll();
}
@PostMapping("/user")
User newUser(@RequestBody User newUser) {
return repository.save(newUser);
}
// Single item
@GetMapping("/user/{id}")
User one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new UserNotFoundException(id));
}
@PutMapping("/user/{id}")
User replaceUser(@RequestBody User newUser, @PathVariable Long id) {
return repository.findById(id)
.map(employee -> {
employee.setEMail(newUser.getEMail());
employee.setPasswordHash(newUser.getPasswordHash());
return repository.save(employee);
})
.orElseGet(() -> {
newUser.setUserID(id);
return repository.save(newUser);
});
}
@DeleteMapping("/user/{id}")
void deleteUser(@PathVariable Long id) {
repository.deleteById(id);
}
}
更新后的放置方法:
@PutMapping(path="/user/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
User replaceUser(@RequestBody User newUser, @PathVariable Long id) {
return repository.findById(id)
.map(employee -> {
employee.setEMail(newUser.getEMail());
employee.setPasswordHash(newUser.getPasswordHash());
return repository.save(employee);
})
.orElseGet(() -> {
newUser.setUserID(id);
return repository.save(newUser);
});
}
现在出现了两个问题。
- 为什么
email
而不是eMail
,做什么来eMail
代替email
- 如何
POST
正确,我做错了什么?
解决方案
“为什么email
而不是eMail
”——这只是杰克逊的默认行为。
“做什么来eMail
代替email
” - 您可以通过 POJO 上的注释来控制杰克逊的行为。这里相关的是@JsonProperty
. 有关详细信息,请参阅此问题。
“如何POST
正确,我做错了什么? ” - 你的意思是PUT
代替POST
,不是吗?定义方法使用的内容类型:
@PutMapping(path="/user/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
User replaceUser(@RequestBody User newUser, @PathVariable Long id) {
...
}
此外,正如@rimonmostafiz 所指出的,您需要重新定义您的curl
电话,转义报价:
curl -X PUT -H "Content-Type: application/json" -d "{ \"email\": \"asd\", \"passwordHash\": \"sad\" }"
顺便说一句:以后请限制自己在每个帖子中回答一个问题。
推荐阅读
- sql - 找出比较同一行中其他列的最小列值
- ios - 如何在 SwiftUI 中检测 TextField 的实时变化?
- c# - 尝试添加新产品时,一个或多个实体的验证失败
- c++ - 在 Windows 上使用 MFC 或 ATL 标头和 MinGW 编译代码?
- python-3.x - Python -- While 循环失败
- java - selenium jre1.7 和 1.8 兼容性冲突
- c#-8.0 - 有什么方法可以通过某些确保它不是的属性来使可空引用推断为非空?
- python - 使用 GridSearchCV 测试多个管道
- vue.js - Vuex:如何更新组件数据或从 Store 调用组件方法
- javascript - 错误:找不到资源 youtube#videoListResponse