gpt4 book ai didi

java - 如何解决Restful WebService中资源不可用的问题

转载 作者:行者123 更新时间:2023-12-01 20:00:06 25 4
gpt4 key购买 nike

我已经使用 POST 方法创建了一个 REST Web 服务“JSONService”(使用 Jersey 实现 JAX-RS)并创建了“JerseyClientPost”。我想要实现的是从 JerseyClientPost 发送 POST。将所有值放入“玩家”对象中并与响应一起发送回来。但是在尝试从 postman 发送 POST 请求时,它会抛出

HTTP Status 404 - Not Found, The requested resource is not available.

任何解决此问题的指示将不胜感激?

请求网址:http://localhost:8080/WeekendSoccer/test/register/create

以下是我的环境详细信息:JDK 1.7.0_79,Tomcat服务器v7.0, Jersey (jaxrs-ri-2.25.1),Web.xml

//REST资源如下

package webService;
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;
import javax.ws.rs.core.Response.ResponseBuilder;
import dto.RegisterPlayer;

@Path("/register")
public class JSONService {
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createPlayerInJSON(RegisterPlayer player) {
String result = "Player Created : " +player;
return Response.status(201).entity(result).build();
}

}

//以下是 Jersey 客户端帖子

package dao;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class JerseyClientPost {
public static void main(String[] args) {

try {

RegisterPlayer player = new RegisterPlayer();
player.setName("Link");
player.setEmail("link@test.com");
player.setCompany("Test Ltd");

Client client = ClientBuilder.newClient(new ClientConfig().register( LoggingFilter.class ));
WebTarget webTarget
= client.target("http://localhost:8080/WeekendSoccer/test");
WebTarget playerWebTarget
= webTarget.path("/register/create");
Invocation.Builder invocationBuilder
= playerWebTarget.request(MediaType.APPLICATION_JSON);

Response response
= invocationBuilder
.post(Entity.entity(player,MediaType.APPLICATION_JSON));

if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));


} catch (Exception e) {

e.printStackTrace();

}

}

}

//RegisterPlayer 模型类,具有 getter、setter

public class RegisterPlayer implements Serializable{    

private String name;
private String email;
private String company;

public RegisterPlayer()
{

}

public RegisterPlayer(String name, String email, String company)
{
super();

this.name = name;
this.email = email;
this.company = company;

}

public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
....
}

//Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WeekendSoccer</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Register Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>webService</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Register Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>JSON Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>dao</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSON Service</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
</web-app>

//列出所有jar

aopalliance-repackaged-2.5.0-b32.jar
gson-2.2.2.jar
hk2-api-2.5.0-b32.jar
hk2-locator-2.5.0-b32.jar
hk2-utils-2.5.0-b32.jar
jackson-annotations-2.3.2.jar
jackson-core-2.3.2.jar
jackson-databind-2.3.2.jar
jackson-jaxrs-base-2.3.2.jar
jackson-jaxrs-json-provider-2.3.2.jar
jackson-module-jaxb-annotations-2.3.2.jar
javassist-3.20.0-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.5.0-b32.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-entity-filtering-2.17.jar
jersey-guava-2.25.1.jar
jersey-media-jaxb.jar
jersey-media-json-jackson-2.17.jar
jersey-server.jar
mysql-connector-java-5.1.40-bin.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar

最佳答案

首先,我从未测试过您的客户端代码,但我猜您的问题与服务器端有关,并在 Postman 中进行了测试。您应该在 pojo 类 RegisterPlayer 中添加 @XmlRootElement 注释,如下所示:

@XmlRootElement
public class RegisterPlayer implements Serializable{

private String name;
private String email;
private String company;

public RegisterPlayer()
{

}

public RegisterPlayer(String name, String email, String company)
{
super();

this.name = name;
this.email = email;
this.company = company;

}

并在pom.xml中添加了这些依赖项

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>

如果你已经有了这些 jar 那就没问题了。它现在对我有用。希望对您有帮助。

jackson-core-2.9.3.jar
jackson-mapper-asl-1.9.13.jar
jackson-core-asl-1.9.13.jar
asm-3.3.1.jar
jersey-bundle-1.19.4.jar
jsr311-api-1.1.1.jar
json-20170516.jar
jersey-server-1.19.4.jar
jersey-core-1.19.4.jar
jersey-multipart-1.19.4.jar
mimepull-1.9.3.jar
jackson-annotations-2.7.0.jar
jackson-databind-2.7.0.jar

关于java - 如何解决Restful WebService中资源不可用的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48300586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com