我有一个简单的hashmap calss,我希望通过Jersey发送为JSON。有没有人有关于如何执行此类操作的示例,我尝试了以下
package de.vogella.jersey.first;
import java.util.HashMap;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.view.Viewable;
// This method is called if XML is request
@GET
@Produces({ MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON })
public HashMap sayXMLHello() {
HashMap map = new HashMap();
map.put( new Integer( 2 ), "two" );
map.put( new Integer( 4 ), "four" );
return map;
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
然后是客户
package de.vogella.jersey.first;
import java.net.URI;
import java.util.HashMap;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class Test {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
// Calls the HashMap function
System.out.println( service.path("rest")
.path("hello")
.accept(MediaType.APPLICATION_JSON)
.get(HashMap.class));
// The HTML
System.out.println(service.path("rest")
.path("hello")
.accept(MediaType.TEXT_HTML)
.get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
}
}
一旦我部署并调用客户端类,我就会收到以下错误:
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/de.vogella.jersey.first/rest/hello
returned a response status of 500 Internal Server Error
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at de.vogella.jersey.first.Test.main(Test.java:25)
所以关于如何使用Jersey将HashMap对象从服务器发送到客户端的任何想法?我也无法将HashMap对象映射到POJO,因为它没有统一的结构(一直在变化)
感谢。
答案 0 :(得分:1)
似乎是一个旧线程,回答它可能对某人有帮助。
首先,正如其他人所说,500错误很可能是由于服务器端代码出现问题而与客户端无关。
要在jersey客户端中获取Map类型的hashmap,请使用GenericType,如下面的代码所示。
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
// Calls the HashMap function
System.out.println( service.path("rest")
.path("hello")
.accept(MediaType.APPLICATION_JSON)
.get(new GenericType<Map<String, String>>() {}));
答案 1 :(得分:0)
首先弄清楚服务器端的异常是什么。这可能比在完全解决问题之前发布问题要好得多解释实际问题。
答案 2 :(得分:0)
这是通过REST服务
发送hashmap数据的示例代码// CLIENT SIDE
package testHashMap;
import java.util.HashMap;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import org.codehaus.jackson.map.ObjectMapper;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class SendHashMap extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request,HttpServletResponse response){
HashMapAcceptModel model = new HashMapAcceptModel();
HashMap<String, String> positionList = new HashMap<String, String>();
positionList.put("Key1", "Value1");
positionList.put("Key2", "Value2");
positionList.put("Key3", "Value3");
model.setCompleteHashMapListList(positionList);
try {
Client client = Client.create();
ObjectMapper mapper = new ObjectMapper();
String JSONData = mapper.writeValueAsString(model);
System.out.println("JSON Object :"+JSONData);
WebResource webResource = client.resource("http://localhost:8080/RESTServer/gtl/json/metallica/JSONHashMapData");
ClientResponse clientResponse = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, JSONData);
System.out.println("Client Response :"+clientResponse.getEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
***********************************************************************************************************************************************************************
// CLIENT SIDE
package testHashMap;
import java.util.HashMap;
public class HashMapAcceptModel {
public HashMap<String, String> completeHashMapListList = new HashMap<String, String>();
public HashMap<String, String> getCompleteHashMapListList() {
return completeHashMapListList;
}
public void setCompleteHashMapListList(HashMap<String, String> completeHashMapListList) {
this.completeHashMapListList = completeHashMapListList;
}
@Override
public String toString() {
return "HashMapAcceptModel [completeHashMapListList="
+ completeHashMapListList + "]";
}
}
***********************************************************************************************************************************************************************
// SERVER SIDE
package testHashMap;
import java.util.HashMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/json/metallica")
public class JSONService {
public static HashMap<String, String> positionList = new HashMap<String, String>();
@POST
@Path("/JSONHashMapData")
@Consumes(MediaType.APPLICATION_JSON)
public Response addLocationMap(HashMapAcceptModel hashmapModel){
System.out.println("result :"+hashmapModel.getCompleteHashMapListList());
String result = "Success";
System.out.println("result :"+result);
return Response.status(201).entity(result).build();
}
}
***********************************************************************************************************************************************************************
// SERVER SIDE
package testHashMap;
import java.util.HashMap;
public class HashMapAcceptModel {
public HashMap<String, String> completeHashMapListList = new HashMap<String, String>();
public HashMap<String, String> getCompleteHashMapListList() {
return completeHashMapListList;
}
public void setCompleteHashMapListList(HashMap<String, String> completeHashMapListList) {
this.completeHashMapListList = completeHashMapListList;
}
@Override
public String toString() {
return "HashMapAcceptModel [completeHashMapListList="
+ completeHashMapListList + "]";
}
}
***********************************************************************************************************************************************************************
您可能需要使用这些库文件
答案 3 :(得分:-1)
Jersey不为HashMap提供MessageBodyWriter。您需要编写自定义MessageBodyWriter。 请参阅this link