gpt4 book ai didi

java - REST api 未从客户端调用 Web 服务

转载 作者:行者123 更新时间:2023-11-30 06:20:24 24 4
gpt4 key购买 nike

我使用 POST 方法创建了一个 Jersey REST Web 服务 'RegisterService'。我想接收 'RegisterClientPost''name''email''company' 的值类并将这些值插入数据库并作为 JSON 响应发回。有人可以帮忙吗,如何从客户端调用 api 并传回服务级别?

Tomcat Server v7.0
Jersey (jaxrs-ri-2.25.1)
MySQL 5 + version


@Path("/register")
public class RegisterService {
private static final String REST_URI = "http://localhost:8080/MyFirstJavaTest/rest/register/postPlayer";
private Client client = ClientBuilder.newClient();
@POST
@Path("/postPlayer")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response createJsonPlayer(WeekendPlayer player) {
return client
.target(REST_URI)
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(player, MediaType.APPLICATION_JSON));
}
}

//如何获取'RegisterClientPost'中的Player值并插入数据库,传入json对象并返回到'RegisterService'

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

try {

String t_Name ="Apple Mango";
String t_Email ="test@example.com";
String t_Comp ="Test Ltd";

Client client = ClientBuilder.newClient();
WebTarget webTarget
= client.target("http://localhost:8080/MyFirstJavaTest/rest");
WebTarget employeeWebTarget
= webTarget.path("/register/postPlayer");
Invocation.Builder invocationBuilder
= employeeWebTarget.request(MediaType.APPLICATION_JSON);
Response response
= invocationBuilder
.post(Entity.entity(inputJson, MediaType.APPLICATION_JSON));

if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}

con.close();
}
catch(Exception e){
e.printStackTrace();
}



}

}

//具有 getter 和 setter 的模型类

public class WeekendPlayer {

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

public WeekendPlayer()
{

}

public WeekendPlayer(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;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}
....

}

//使用Web.xml

<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>com.myfirstjavatest.pkg</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>

postman 收到错误:服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持。

最佳答案

您的资源定义了以下方法,该方法需要 URL 编码形式的 HTTP POST(请参阅@Consumes(MediaType.APPLICATION_FORM_URLENCODED))

@POST
@Path("/postPlayer")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response createJsonPlayer(WeekendPlayer player) {
return Response.ok();
}

但是您的 JAX-RS 客户端使用 MediaType.APPLICATION_JSON 发送 HTTP POST。这就是您收到错误的原因。您需要将注释更改为 @Consumes(MediaType.APPLICATION_JSON)

@POST
@Path("/postPlayer")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createJsonPlayer(WeekendPlayer player) {
return Response.ok("It works!");
}

关于java - REST api 未从客户端调用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48275381/

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