英文:
Failing to get a POST request body
问题
以下是翻译好的部分:
package com.pipa.api;
import lombok.Builder;
import lombok.Value;
@Value
@Builder
public class PlayerData {
private Integer userId;
}
package com.pipa.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class Application {
private static ObjectMapper objectMapper;
public static void main(String[] args) throws IOException {
int serverPort = 8000;
HttpServer server = HttpServer.create(new InetSocketAddress(serverPort), 0);
server.createContext("/post", (exchange -> {
if ("POST".equals(exchange.getRequestMethod())) {
PlayerData playerData = objectMapper.readValue(exchange.getRequestBody(), PlayerData.class);
System.out.println("this print never appear");
} else {
exchange.sendResponseHeaders(405, -1); // 405 Method Not Allowed
}
exchange.close();
}));
server.setExecutor(null); // creates a default executor
server.start();
}
}
请注意,这是您提供的代码的翻译版本。如果您有任何问题或需要进一步的帮助,请随时提问。
英文:
I'm trying to get a JSON from the body of a request post method (using insomnia/postman), but I have no progress doing this. I did a class called PlayerData, which have only one attribute (userId). I'm using Jackson library to use readValue method, in order to map my json body to my java class PlayerData. To be able to see what is happening, I been have putting a print log and breakpoints to debug what could be, but for some reason, both of them are skipped by my code. I don't return a response, because in this case I don't want to. The idea here is only to set a instance of PlayerData with userId from body request, with no need to persist data on disk.
PlayerData.class
package com.pipa.api;
import lombok.Builder;
import lombok.Value;
@Value
@Builder
public class PlayerData {
private Integer userId;
}
Application.java
package com.pipa.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class Application {
private static ObjectMapper objectMapper;
public static void main(String[] args) throws IOException {
int serverPort = 8000;
HttpServer server = HttpServer.create(new InetSocketAddress(serverPort), 0);
server.createContext("/post", (exchange -> {
if ("POST".equals(exchange.getRequestMethod())) {
PlayerData playerData = objectMapper.readValue(exchange.getRequestBody(), PlayerData.class);
System.out.println("this print never appear");
} else {
exchange.sendResponseHeaders(405, -1); // 405 Method Not Allowed
}
exchange.close();
}));
server.setExecutor(null); // creates a default executor
server.start();
}
}
答案1
得分: 0
在以下代码行 objectMapper.readValue(exchange.getRequestBody(), PlayerData.class);
发生了某些情况。
exchange.getRequestBody()
返回一个 InputStream 实例。我尝试将 InputStream 转换为字符串,这样做起来效果很好。我还使用了 net.sf.json.JSONObject 来解析 JSON 字符串,然后将其转换为 PlayerData 类的对象。如果您使用这种方法,代码执行会到达最后的打印语句。
InputStream inputStream = exchange.getRequestBody();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
String theString = writer.toString();
System.out.println(theString);
JSONObject jsonObject = JSONObject.fromObject(theString);
PlayerData pd = (PlayerData) JSONObject.toBean(jsonObject, PlayerData.class);
System.out.println(pd.toString());
这只是一个权宜之计,因为我仍然不理解 objectMapper
问题背后的原因。
英文:
There is something happening at the line objectMapper.readValue(exchange.getRequestBody(), PlayerData.class);
exchange.getRequestBody()
returns an instance of InputStream. I tried to convert the InputStream to string and it worked fine. I also used net.sf.json.JSONObject to parse the JSON string and then converted it into an object of class PlayerData. If you use this approach the code execution goes to the last print statement.
InputStream inputStream = exchange.getRequestBody();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer );
String theString = writer.toString();
System.out.println(theString);
JSONObject jsonObject = JSONObject.fromObject(theString);
PlayerData pd = (PlayerData) JSONObject.toBean(jsonObject, PlayerData.class);
System.out.println(pd.toString());
This is just a workaround as I still don't understand the reason behind the issue with objectMapper
.
答案2
得分: 0
有代码存在两个问题。
-
private static ObjectMapper objectMapper;
从未被初始化。因此,您将会得到 NullPointerException 错误。
尝试使用private static ObjectMapper objectMapper = new ObjectMapper();
-
如果您使用了 Lombok 和 Jackson,您可能需要按照这个链接中提到的进行更改。
英文:
There are 2 issues with the code.
-
private static ObjectMapper objectMapper;
is never initialized. Therefore you will get NullPointerException.
Tryprivate static ObjectMapper objectMapper = new ObjectMapper();
-
Using Lombok and Jackson, you may need to make changes as mentioned in this link
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论