对象值在传递给 JAX-RS 中的 servlet 时变为 NULL
Object values becomes NULL while passing to the servlet in JAX-RS
使用 Jersey 的 JAX-RS
我正在尝试将对象 "Broadcast" 从客户端发送到 servlet 并在 servlet 端打印值。但是我的输出变为空。帮我解决这个问题提前谢谢:-)
Servlet
@POST
@Path("/receiverMessage")
public Response sendMessage(Broadcast broadcast) {
System.out.println(broadcast.getSenderName());
System.out.println(broadcast.getReceiverName());
System.out.println(broadcast.getTime());
return Response.ok().status(200).entity("SUCCESS").build();
}
客户端
public class Client {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Broadcast broadcast = new Broadcast("SENDER", "RECEIVER", new Timestamp(System.currentTimeMillis()));
Response messagePost = client.target("http://localhost:8081/xyz/abc")
.path("receiverMessage")
.request()
.post(Entity.entity(broadcast, MediaType.APPLICATION_JSON), Response.class);
System.out.println(messagePost.getStatus());
}}
广播模型
public class Broadcast {
private String senderName;
private String receiverName;
private Timestamp time;
}
MessageBodyWriter
@Override
public void writeTo(Broadcast broadcast, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
System.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");
try {
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.marshal(broadcast, entityStream);
} catch (JAXBException e) {
e.printStackTrace();
}
}
输出
null
null
null
如果您使用的是 Jersey,则无需执行任何额外操作。 Jersey 有能力(反)序列化你的 JSON。但是,您需要做一些更改。
您在服务器端缺少 @Consumes
和 @Produces
注释
@POST
@Path("/receiverMessage")
@Consumes ( { MediaType.APPLICATION_JSON })
@Produces ( { MediaType.TEXT_PLAIN })
public Response sendMessage(Broadcast broadcast) {
System.out.println(broadcast.getSenderName());
System.out.println(broadcast.getReceiverName());
System.out.println(broadcast.getTime());
return Response.ok().status(200).entity("SUCCESS").build();
}
- 此外,您的 POJO class 没有注释表明它是 JAXB class。你可以找到很好的例子 Using JAX-RS with JAXB
使用 Jersey 的 JAX-RS
我正在尝试将对象 "Broadcast" 从客户端发送到 servlet 并在 servlet 端打印值。但是我的输出变为空。帮我解决这个问题提前谢谢:-)
Servlet
@POST
@Path("/receiverMessage")
public Response sendMessage(Broadcast broadcast) {
System.out.println(broadcast.getSenderName());
System.out.println(broadcast.getReceiverName());
System.out.println(broadcast.getTime());
return Response.ok().status(200).entity("SUCCESS").build();
}
客户端
public class Client {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Broadcast broadcast = new Broadcast("SENDER", "RECEIVER", new Timestamp(System.currentTimeMillis()));
Response messagePost = client.target("http://localhost:8081/xyz/abc")
.path("receiverMessage")
.request()
.post(Entity.entity(broadcast, MediaType.APPLICATION_JSON), Response.class);
System.out.println(messagePost.getStatus());
}}
广播模型
public class Broadcast {
private String senderName;
private String receiverName;
private Timestamp time;
}
MessageBodyWriter
@Override
public void writeTo(Broadcast broadcast, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
System.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");
try {
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.marshal(broadcast, entityStream);
} catch (JAXBException e) {
e.printStackTrace();
}
}
输出
null
null
null
如果您使用的是 Jersey,则无需执行任何额外操作。 Jersey 有能力(反)序列化你的 JSON。但是,您需要做一些更改。
您在服务器端缺少
@Consumes
和@Produces
注释@POST @Path("/receiverMessage") @Consumes ( { MediaType.APPLICATION_JSON }) @Produces ( { MediaType.TEXT_PLAIN }) public Response sendMessage(Broadcast broadcast) { System.out.println(broadcast.getSenderName()); System.out.println(broadcast.getReceiverName()); System.out.println(broadcast.getTime()); return Response.ok().status(200).entity("SUCCESS").build(); }
- 此外,您的 POJO class 没有注释表明它是 JAXB class。你可以找到很好的例子 Using JAX-RS with JAXB