首页 > 解决方案 > 春季启动中的“不支持的媒体类型” - 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这个命令时,它给了我这个:

spring-boot curl 获取

这是非常正确的,虽然它给了我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);
            });
}

现在出现了两个问题

标签: javaspringspring-bootcurl

解决方案


为什么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\" }"

顺便说一句:以后请限制自己在每个帖子中回答一个问题。


推荐阅读