嗨,我正在努力解决这个错误。我有我的apimanager
public static ProfileEntity createUser(UserEntity userEntity) {
// userEntity - userRequestModel
UserRequestModel uRequestModel = InterviewerMapper.convertRequest(userEntity,MapperImplementation::convertRequest);
try {
WebTarget Client = ClientBuilder.newClient().target("http://localhost:8080/messenger/webapp");
Entity entity = Entity.entity(uRequestModel, MediaType.APPLICATION_JSON);
Response response = Client.path("/interview/users").request().post(entity);
UserResponseModel userResponseModel = response.readEntity(UserResponseModel.class);
ProfileEntity pEntity = InterviewerMapper.convertProfile(userResponseModel, e -> {
try {
return MapperImplementation.convertProfile(e);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
});
return pEntity;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我的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>ca.ciccc.madp202</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</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.7</source>
<target>1.7</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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.22</version>
<scope>test</scope>
</dependency>
<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>
<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId> </dependency> -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
我的资源:
@POST
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public UserResponseModel createUser(UserRequestModel userRequest) {
Random rand = new Random();
int userId = rand.nextInt(50) + 1;
// insert into database and retrieve the userId
UserResponseModel uResponseModel = new UserResponseModel(userRequest.getFirstName(), userRequest.getLastName(), userRequest.getCountry(), userRequest.getUserName(), userRequest.getPassWord(), userId , generateAuthToken(), new Date());
return uResponseModel;
}
和我的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>ca.ciccc.madp202.messenger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapp/*</url-pattern>
</servlet-mapping>
</web-app>
这是我的请求类
package ca.ciccc.madp202.messenger.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class UserRequestModel {
private String firstName;
private String lastName;
private String country;
private String userName;
private String passWord;
public UserRequestModel() {
}
public UserRequestModel(String firstName, String lastName, String country, String userName, String passWord) {
this.firstName = firstName;
this.lastName = lastName;
this.country = country;
this.userName = userName;
this.passWord = passWord;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
我的回复课
package ca.ciccc.madp202.messenger.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement 公共类UserResponseModel {
private String firstName;
private String lastName;
private String country;
private String userName;
private String passWord;
private Integer userID;
private String authtoken;
private Date joinDate;
public UserResponseModel() {
}
public UserResponseModel(String firstName, String lastName, String country, String userName, String passWord,
Integer userID, String authtoken, Date joinDate) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.country = country;
this.userName = userName;
this.passWord = passWord;
this.userID = userID;
this.authtoken = authtoken;
this.joinDate = joinDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public String getAuthtoken() {
return authtoken;
}
public void setAuthtoken(String authtoken) {
this.authtoken = authtoken;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
}
我用Google搜索了问题,大多数解决方案都是添加此依赖项
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.25.1</version>
</dependency>
但我已经拥有它了。我不知道出了什么问题。有人可以帮帮我吗? 我已经用邮递员测试了我的资源文件,看看我是否得到了任何响应并且它正在工作; Link to postman