在我的小项目中编码我现在拥有的内容:
主要课程:
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
你能帮助我吗?或者有人有任何想法?谢谢。答案 0 :(得分:0)
我发送的JSON不正确,所以我更改了所有内容