REST api不是从客户端调用到Web服务

时间:2018-01-16 06:40:54

标签: java rest jersey

我使用Jersey REST方法创建了'RegisterService'网络服务POST。我希望在'name'类中收到'email''company''RegisterClientPost'的值,并将这些值插入数据库并以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:收到错误 服务器拒绝了此请求,因为请求实体所采用的方法所请求的资源不支持该格式。

1 个答案:

答案 0 :(得分:1)

您的资源定义了以下预期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!");
}