简体   繁体   中英

Jersey+grizzly2 standalone server JSON parse error

Code what i have now in my little project:

Main class:

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>

Resource class:

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 entity class:

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;
    }

Then Im starting server and trying to send POST request via PostaMan, please see this link

Exception log: at pastebin

Amigos can you please help me? or maybe someone has any ideas? Thank you.

我发送了不正确的JSON,因此我对其进行了更改并且一切正常

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM