代码之家  ›  专栏  ›  技术社区  ›  Maifee Ul Asad

spring引导中的“不支持的媒体类型”

  •  0
  • Maifee Ul Asad  · 技术社区  · 5 年前

    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")));
        };
    }
    

    这给了我:

    spring boot pre-loading

    curl -v localhost:8080/user 这个命令给了我这个:

    spring-boot curl get

    这是非常正确的,虽然它给了我 email eMail .

    但是当我给

    curl -X PUT localhost:8080/user/3 -H 'Content-type:application/json' -d '{"passwordHash":"12345","email":"admin1@admin.com"}'
    

    spring-boot curl post

    这太可怕了。我在跟踪 this

    这是我的 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);
                });
    }
    

    .

    • 为什么? 而不是 电子邮件 电子邮件 而不是 电子邮件
    • 如何 POST
    0 回复  |  直到 5 年前
        1
  •  3
  •   Turing85 Oleksandr Pyrohov    5 年前

    为什么? email 而不是 eMail “-这只是杰克逊的默认行为。

    而不是 电子邮件 “-您可以通过POJO上的注释来控制Jackson的行为。这里的相关问题是 @JsonProperty . 看见 this question 详情请参阅。

    " 如何 POST “-你是说 PUT 而不是 是吗?定义方法使用的内容类型:

    @PutMapping(path="/user/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
    User replaceUser(@RequestBody User newUser, @PathVariable Long id) {
        ...
    }
    

    curl

    curl -X PUT -H "Content-Type: application/json" -d "{ \"email\": \"asd\", \"passwordHash\": \"sad\" }"
    

    顺便说一句:请 limit yourself to one question per post 未来

        2
  •  -1
  •   ScanQR    5 年前

    添加缺失 consumes 归因于 @PutMapping

    @PutMapping(path= "/user/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    User replaceUser(@RequestBody User newUser, @PathVariable Long id) {
    

    虽然它给我的是电子邮件而不是电子邮件

    这完全取决于你的工作 getter/setter eMail 在你的 User 实体。我想你的能手一定是 getEmail()