【问题标题】:Passing Json to Rest and receiving it through JSONObject将 Json 传递给 Rest 并通过 JSONObject 接收
【发布时间】:2016-01-26 13:38:09
【问题描述】:

我有以下服务器端代码:-

    import org.json.JSONObject;
    @Path("/user")
    public class Users {

    @POST
    @Path("register")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response registerUser(JSONObject userDetails) {
        return Response.status(Status.ACCEPTED).entity("User Created.Details are: " + userDetails).build();
    }
}

一旦我尝试使用以下方法调用它,我就会收到 415 错误。你能告诉我有什么办法解决这个问题吗?

{
    "user_id": "masha@meeshka.com",
    "password": "mashakawa",

    "user_profile": {
        "name": "Masha",
        "city": "New York",
        "email": "maasha@kawa.com",
        "age": 20
    },

    "user_settings": {
        "phone_number": "+91898342123"
    }
}

顺便说一句,我使用的是泽西岛。

【问题讨论】:

  • 也许您使用媒体类型“text/plain”而不是“application/json”发送请求?
  • 应该是Response.status(Status.ACCEPTED).entity(userDetails).build(); "User Created.Details are: " + userDetails 将字符串与 userDetails 连接将使对象字符串不是 bean 对象。

标签: java json web-services rest jersey


【解决方案1】:

在 JAX-RS 中,您必须创建一个 JSON MessageBodyReader,它将 readFrom InputStream 并返回一个 JSONObject

@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class JSONObjectMessageBodyReader implements MessageBodyReader<JSONObject> {

    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    public JSONObject readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws java.io.IOException, javax.ws.rs.WebApplicationException {
        //Using entityStream, read the content and return a  JSONObject back...
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(entityStream, "UTF-8")); 
        StringBuilder responseStrBuilder = new StringBuilder();

        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        return new JSONObject(responseStrBuilder.toString());
    }
}

【讨论】:

  • @user1939366 我不知道你不能把InputStream 带到JSONObject。我以为你可以。请查看我更新的帖子。
【解决方案2】:

一个简单的方法是--

public void parseJson(WebResource service) throws UniformInterfaceException, JSONException {

  JSONArray resultArray = new JSONArray(service.path("resources/test/json/Tom").get(String.class));

  List<User> userList = new ArrayList<User>();

  for (int count = 0; count < resultArray.length(); count++) {

    JSONObject userObject = resultArray.getJSONObject(count);
    // this is tor read a sample JSON., this code will change according to your need.
    User user = new User(userObject.get("name").toString(), userObject.get("surname").toString(), userObject.get("address").toString()); 

    userList.add(user);

  }

  System.out.println(userList);

 }

此代码是专门为 Jersey Web 服务编写的。

请在泽西岛找到更多编码链接 -

Creating Webservices with Maven and Jersy

编辑:(客户端在其类路径中需要 java-json.jar 来开发此代码)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 2014-02-26
    • 2017-12-13
    • 1970-01-01
    • 2018-02-13
    • 2011-01-23
    • 2012-10-27
    • 2012-04-12
    相关资源
    最近更新 更多