【问题标题】:Jersey+grizzly2 standalone server JSON parse errorJersey+grizzly2 独立服务器 JSON 解析错误
【发布时间】:2014-08-25 19:33:59
【问题描述】:

编写我现在在我的小项目中的代码:

主类:

package main;

import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;
import org.glassfish.grizzly.http.server.HttpServer;
import utils.text.Messages;
import utils.text.Props;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;


public class Main {

    public static URI BASE_URI;

    private static void getBaseURI(int port) {
        BASE_URI = UriBuilder.fromUri(Props.BASE_URL).port(getPort(port)).build();
    }

    private static int getPort(int defaultPort) {
        String port = System.getProperty(Props.JERSEY_TEST_PORT);
        if (null != port) {
            try {
                return Integer.parseInt(port);
            } catch (NumberFormatException e) {
            }
        }
        return defaultPort;
    }

    protected static HttpServer startServer() throws IOException {
        final Map<String, String> initParams = new HashMap<String, String>();
        initParams.put("com.sun.jersey.config.property.packages", "rest");
        initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
        return GrizzlyWebContainerFactory.create(BASE_URI, initParams);
    }

    public static void main(String[] args) throws IOException, java.text.ParseException {


                getBaseURI(11111);
                final HttpServer httpServer = startServer();
                System.out.println(Messages.STARTING_GRIZZLY + BASE_URI);
                System.in.read();
                httpServer.stop();
    }
}

pom.xml:

 <dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly2</artifactId>
        <version>1.9.1</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9.1</version>
    </dependency>

</dependencies>

资源类:

import utils.text.RestResources;
import utils.text.RestResponse;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path(RestResources.MAIN_REST_PATH)
public class ClientDisconnectRest {


    @POST
    @Path("/client_disconnect")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createTrackInJSON(LoginData cld) {
        try {
            return Response.status(200).entity(RestResponse.OK).build();
        } catch (Exception e) {
            return Response.status(400).entity(RestResponse.ERROR).build();
        }

    }
}

LoginData 实体类:

import javax.ws.rs.QueryParam;

public final  class LoginData {


    private  String login;
    private  String password;

    public LoginData(
            @QueryParam("login") final String login,
            @QueryParam("password") final String password) {

       // require(login != null, "login", login);
       // require(password != null, "password", password);

        this.login = login;
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

然后我启动服务器并尝试通过 PostaMan 发送 POST 请求,请参阅link

异常日志:at pastebin

朋友们能帮帮我吗?或者也许有人有任何想法?谢谢。

【问题讨论】:

  • 你发送的json是什么?
  • 我对 Postman 插件没有任何经验,但是您确定它只是因为您将内容类型标头指定为 json 而将表单数据发送为 json 吗?

标签: java json jersey jersey-2.0 grizzly


【解决方案1】:

我发送了不正确的 JSON,所以我更改它并且一切正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多