gpt4 book ai didi

web-services - 请求实体不能为空

转载 作者:行者123 更新时间:2023-12-03 15:46:08 25 4
gpt4 key购买 nike

I am new to Jax-RS. I am trying to implement a simple GET method using jersey. I am getting the correct output for the Collection resource, but I am getting "ERROR 400 Bad Request" for instance resource . I am stuck at this point. Everything seems to be correct,but I am missing something which I am not able to figure out. I have been trying it for last 10 days. Any help will be appreciable. Please find my code below.I am using POSTMAN rest client which is throwing "Unexpected 'T'" as response and ARC is throwing "The request entity cannot be empty." and both are Error 400 bad request.



这是我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>MessangerWithoutMaven</display-name>
<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.manish.jax_rs.Messanger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

这是我的 MessageResource.java :
package com.manish.jax_rs.Messanger.resources;

import java.util.List;

import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.manish.jax_rs.Messanger.model.Message;
import com.manish.jax_rs.Messanger.service.MessageService;

@Path("/messages")
public class MessageResource {

MessageService messageService = new MessageService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Message> getMessages(){

return messageService.getAllMessages();
}

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Message getMessage(@PathParam("id") int id){


return messageService.getMessage(id);
}

}

这是 DaoClass.java:
package com.manish.jax_rs.Messanger.Dao;

import java.util.HashMap;
import java.util.Map;

import com.manish.jax_rs.Messanger.model.Message;


public class DaoClass {


private static Map<Integer,Message> messages = new HashMap<Integer,Message>();


public static Map<Integer, Message> getMessages() {
return messages;
}
}

这是 MessageService.java:
package com.manish.jax_rs.Messanger.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.manish.jax_rs.Messanger.Dao.DaoClass;
import com.manish.jax_rs.Messanger.model.Message;

public class MessageService {

private Map<Integer,Message> messages= DaoClass.getMessages();

Message m1= new Message(1,"Hello John","John");
Message m2= new Message(2,"Hello Mathews","Mathews");
Message m3= new Message(3,"Hello Albert","Albert");

public MessageService(){
messages.put(1,m1);
messages.put(2,m2);
messages.put(3, m3);
}
public List<Message> getAllMessages(){
return new ArrayList<Message>(messages.values());
}
public Message getMessage(int id){
return messages.get(id);
}
}

这是 Message.java:
package com.manish.jax_rs.Messanger.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Message {

private int id;
private String message;
//private Date created;
private String author;

public Message() {

}
public Message(int id, String message, String author) {

this.id = id;
this.message = message;
this.author = author;
//this.created = new Date();
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}

public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}



[List of jars][1]




[correct output for collection resource "messages" in Postman rest client][2]


[Incorrect output for instance resource /messages/1][3]enter code here


[1]: /image/sWfMl.jpg
[2]: /image/y5Nnf.jpg
[3]: /image/8CwId.jpg

最佳答案

请检查您的 PathParam 导入。
而不是“javax.websocket.server.PathParam;”使用“导入 javax.ws.rs.PathParam;”。
这应该可以正常工作。

关于web-services - 请求实体不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42912182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com