我的问题是,当我try 这一点时,我得到了一个媒体类型错误,然后我更改了标题.现在我收到一个500错误.问题不在于api,它在postman上运行得很好,我在请求帖子时是否在代码中出错?

我的对象模型

private String module;
    private String notificationGroupType;
    private String notificationGroupCode;
    private String notificationType;
    private String inLineRecipients;
    private String eventCode;
    private HashMap<String, Object> metaData;

    public EmailModel() {
        this.module = "CORE";
        this.notificationGroupType ="PORTAL";
        this.notificationGroupCode = "DEFAULT";
        this.notificationType =  "EMAIL";
        this.inLineRecipients ="[chrispotjnr@gmail.com,chris@mqattach.com]";
        this.eventCode = "DEFAULT";
        this.metaData = metaData;
    }
 

我的控制器

 private EmailModel em;
    @RequestMapping(value = "test", method = RequestMethod.Post)
    public void post() throws Exception {
        String uri= "TestUrl";


        EmailModel em = new EmailModel();
                EmailModel data =em;

        HttpClient client = HttpClient.newBuilder().build();
        HttpRequest request = HttpRequest.newBuilder()
                .headers("Content-Type", "application/json")
                .uri(URI.create(uri))
                .POST(HttpRequest.BodyPublishers.ofString(String.valueOf(data)))
                .build();

        HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        System.out.println(em);
        System.out.println(response.statusCode());

    }

postmanImage

推荐答案

您必须在ObjectMapper之前将EmailModel转换为json格式

ObjectMapper objectMapper = new ObjectMapper();
String data = objectMapper
      .writerWithDefaultPrettyPrinter()
      .writeValueAsString(em);

POST改为:

.POST(HttpRequest.BodyPublishers.ofString(data))

更多关于ObjectMapper

Java相关问答推荐

最小拓Flutter 排序的时间复杂度是多少?

如何在Java中声明未使用的变量?

Android Studio—java—在onBindViewHolder中,将断点和空白添加到BackclerView中

条件加载@ManyToMany JPA

我找不到&Quot;配置&的位置

JavaFX如何在MeshView中修复多个立方体?

将Spring Boot 3.2.0升级到3.2.1后查询执行错误

从ActiveMQ Classic迁移到ActiveMQ Artemis需要进行哪些客户端更改?

Java中将文本拆分为数字或十进制数字和字符串

使用用户引入的参数生成人员数组

使用存储在字符串变量中的路径目录打开.pdf文件

Dijkstra搜索算法的实现

错误:不兼容的类型:Double不能转换为Float

Java在操作多个属性和锁定锁对象时使用同步和易失性

记录是类的语法糖吗?

有没有办法在o(log(N))中以系统的方式将数组中的小块元素复制和移动到新增长的数组中的左侧?

控制器建议异常处理

rest api服务 spring 启动中出现IllegalFormatConversionException

Spring Boot Security-每个端点都被403禁止,Spring记录一个BasicErrorController#错误(HttpServlet请求)

@此处不能应用可为null的批注