客户端将json传递给Web服务运动衫中的服务器(没有maven)

时间:2015-04-20 13:29:13

标签: java json rest

我正在尝试将json从客户端程序(WebService创建为webapplication)传递给我的Web服务.Client程序是用核心java编写的。基于http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/教程我编写了我的客户端程序。我需要知道如何运行我的程序。(使用JERSEY NOT MAVEN)。在没有客户端的时候我硬编码json并获得良好的响应。但在这种情况下添加客户端时,客户端程序包含main()方法,我很困惑,需要知道怎么运行它。我会把我的代码用于进一步理解。任何人都可以提供关于我的dobt的文档吗?我是这个环境的新手...

Postjson.java

public class Postjson {
        public static void main(String[] args) {
            System.out.println("inside postjson classfor posting request");
             try {
//               Student st = new Student("Adriana", "Barrer", 12, 9);
Clientdetails cld=new Clientdetails("request","AuthenticationType","test@gmail.com",12345,"null","null");
//               String JSON_DATA =
//                       "{" 
//                         + "  \"SnapshotRequest\": [" 
//                         + "    {" 
//                         + "      \"AuthenticationType\": \"email\"," 
//                         + "      \"EmailAddress\": \"test@gmail.com\","                  
//                         + "      \"Password\" : \"12345\"," 
//                         + "      \"PracticeID\" : \"null\"," 
//                         + "      \"DeviceID\" : \"null\""
//                         + "    } +   ]"
//                     + "}";
//                Clientdetails cld=new Clientdetails();
//                cld.setUsername("test@gmail.com");
//                cld.setPassword("password");
//                cld.setDevice_id("12345");
//                           Student st = new Student("Adriana", "Barrer", 12, 9);
                                ClientConfig clientConfig = new DefaultClientConfig();
                                clientConfig.getFeatures().put(
                 JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
                                Client client = Client.create(clientConfig);
                 WebResource webResource = client
                 .resource("http://localhost:9090/Snapshothealthapp1/rest/customers/get");
                 System.out.println("after url step");
                 ClientResponse response = webResource.accept("application/json")
                         .type("application/json").post(ClientResponse.class, cld);
                 System.out.println("after passing json object");
                 if (response.getStatus() != 200) {

                                 throw new RuntimeException("Failed : HTTP error code : "

                                         + response.getStatus());

                             }



                             String output = response.getEntity(String.class);



                             System.out.println("Server response .... \n");

                             System.out.println(output);



                         } catch (Exception e) {



                             e.printStackTrace();



                         }

        }

Authentication.java

   import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Request;
    import javax.ws.rs.core.Response;

    import com.snapshothealthapp1.client.Clientdetails;
    import com.snapshothealthapp1.model.AuthenticationModel;
    @Path("/customers")
    public class Authentication {
        @POST
        @Path("/get")
        @Consumes(MediaType.APPLICATION_JSON)
        public Response getRequestUrl(Clientdetails s) {
            //TODO:Create a JSON(recieved from client request)
    //      JSONObject jsonClient = new JSONObject(); 
            System.out.println("inside Authentication");

             String output = s.toString();

             return Response.status(201).entity(output).build();
        }

的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" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>

     <init-param>

      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.snapshothealthapp1.controller</param-value>
    </init-param>
    <init-param>

        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>

        <param-value>true</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

如何使用main方法和另一种资源方法运行单个项目。如果不可能,请告诉辅助方法吗? 我保持我的服务器并运行main方法(客户端代码)。但它给了我错误。 错误发布在

之下
com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.snapshothealthapp1.client.Clientdetails, and MIME media type, application/json, was not found
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
    at com.sun.jersey.api.client.Client.handle(Client.java:648)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563)
    at com.snapshothealthapp1.client.Postjson.main(Postjson.java:40)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.snapshothealthapp1.client.Clientdetails, and MIME media type, application/json, was not found
    at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
    ... 5 more

当我对这个领域不熟悉时,我对这种方式是否正确感到困惑。任何人都可以帮助我。任何帮助都会非常值得注意....

1 个答案:

答案 0 :(得分:0)

根据您链接的教程,它使用此Maven依赖项来支持JSON / POJO。

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.9</version>
</dependency>

这种依赖性带来了许多其他依赖关系(这就是我们使用Maven的原因: - )

所以你需要确保拥有以下所有的罐子

enter image description here

您可以搜索并下载them here

你拥有的其他一切看起来都很好。