我试图在java中使用Jersey-client发送一个json Post,但它无效。
无效的代码
Client client = ClientBuilder.newClient();
JSONObject jsonObject = new JSONObject();
jsonObject.put("userName", "euler");
jsonObject.put("password", "password");
Response response = client.target("http://localhost:8080/ldap/webapi/Auth").request(MediaType.APPLICATION_JSON).post(Entity.json(jsonObject));
String result = response.readEntity(String.class);
对于上面的代码,我得到的结果字符串为(false)
现在我创建了一个模型POJO类user.java,其userName和password作为变量,并将用户对象作为json发送,并开始工作。
正在运作的代码
Client client = ClientBuilder.newClient();
User user = new User();
user.setUserName("euler");
user.setPassword("password");
Response response = client.target("http://localhost:8181/ldapAuth/webapi/Auth").request(MediaType.APPLICATION_JSON).post(Entity.json(user));
String result = response.readEntity(String.class);
对于上面的代码,我得到的结果字符串为(true)。
我需要将POST作为json对象发送,而不是将其转换为JSON的模型类。
REST API:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Status Login(User user){
AuthManager manager = new AuthManager();
return manager.Authenticate(user);
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.wooqer.com.ldapAuth</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.wooqer.com</groupId>
<artifactId>ldapAuth</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ldapAuth</name>
<build>
<finalName>ldapAuth</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.6</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>