未找到 Java 类型 class org.json.JSONObject 和 MIME 媒体类型 application/json 的邮件正文编写器

A message body writer for Java type, class org.json.JSONObject, and MIME media type, application/json, was not found

我针对同一个问题检查了很多解决方案,但没有解决任何问题。这就是我再次发帖的原因。 当我使用 POSTMAN 时,它工作正常。

我也尝试过不编码。然后使用

"application/json; charset=utf-8" No any solution I got.

我的客户代码:

  public static void main(String[] args) throws JSONException, IOException 
            {
                Test1 my_client = new Test1();
                File file_upload = new File("C:/MyJSON.txt");
                my_client.sendFileJSON(file_upload);
            }


            private void sendFileJSON(File file_upload) throws JSONException, IOException{

                ClientConfig config = new DefaultClientConfig();
               // config.getClass().add(MOXyJsonProvider.class);
                Client client = Client.create(config);
                client.addFilter(new LoggingFilter());
                WebResource service = client.resource("https://hostedactivation.com/XXXXX/XXXXXXXX.php");


                JSONObject data_file = new JSONObject();
                data_file.put("file_name", file_upload.getName());

                data_file.put("file", convertFileToString(file_upload));
                ClientResponse client_response = service.type(MediaType.APPLICATION_JSON).header("Authorization", "Basic Y3lDi543XJzcEFsMkJ1Fm0xV2Ic5").post(ClientResponse.class, data_file);
                System.out.println("Status: "+client_response.getEntity(String.class));

                client.destroy();
            }

//Convert my file to a Base64 String
        private String convertFileToString(File file) throws IOException{
            byte[] bytes = Files.readAllBytes(file.toPath());   
            return new String(Base64.encode(bytes));
        }

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>DataAnalytics</groupId>
    <artifactId>DataAnalytics</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    <dependency>
         <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
         <version>1.9.1</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.19</version>
</dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-all</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
        <!--  <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>1.2</version>
    </dependency> -->
    </dependencies>
</project>

MyJSON.txt

{
"header":{
"user":"XXXX",
"password": "XXXXXXXXXXXXXX",
"table":"licf",
"method_override":"get"
}
}

我不知道,我缺少一些东西。欢迎任何意见。谢谢。

public static void main(String[] args) throws IOException {

        Parent  p = new Parent();
        Header h = new Header();
        h.setUser("XXX");
        h.setPassword("af962d0ceacdd9af");
        h.setTable("licf");
        h.setMethod_override("get");
        p.setHeader(h);

        Test t = new Test();
        String jsonInString = t.writeJSON(p);

         ClientConfig config = new DefaultClientConfig();
         Client client = Client.create(config);
            client.addFilter(new LoggingFilter());
            WebResource service = client.resource("https://hostedactivation.com/XXXXX/XXXXXXXX.php");

            ClientResponse client_response = service.type(MediaType.APPLICATION_JSON).
                    header("Authorization", "Basic Y3liZXJzcGFI3UGc5").
                    post(ClientResponse.class,jsonInString);

            System.out.println("Status: "+client_response.getEntity(String.class));

            client.destroy();
}
    private String writeJSON(Parent p) throws JsonGenerationException, JsonMappingException, IOException{
        ObjectMapper mapper = new ObjectMapper();   
        String jsonInString = mapper.writeValueAsString(p);
        return  jsonInString;
       }