This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 258639

Summary: RESTful Java client Incorrect code generation - Produces and consumes content-types are mismatching
Product: webservices Reporter: vidhyadharantechdays <vidhyadharantechdays>
Component: RESTAssignee: Milan Kuchtiak <mkuchtiak>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.1   
Hardware: PC   
OS: Windows 7   
Issue Type: DEFECT Exception Reporter:

Description vidhyadharantechdays 2016-04-04 07:13:08 UTC
This is my RestService

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vidhya.java.http.patch.jax.rs;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import org.json.simple.JSONObject;

/**
 *
 * @author Vidhyadharan Deivamani (it.vidhyadharan@gmail.com)
 */
@Path("restws")
public class RestWs {
 @Context
private UriInfo context;

public RestWs() {
}


@GET
@Produces("application/json")
public String getJson() {
    return ("{\"pesan\":\"hello\"}");
}


@PUT
@Produces("text/plain")
@Consumes("application/json")
public String putJson(String content) {
    return("Content yang didapat : "+content);
}

@Path("/mahasiswaData/{id}")
@GET
@Produces("application/json")
public String getMahasiswaByID(@PathParam("id")String nim)
{
    JSONObject jo = new JSONObject();
    jo.put("id", nim);
    jo.put("nama", "Budi");
    return(jo.toJSONString());
}

@Path("/mahasiswaData/{id}")
@POST
@Consumes("text/plain")
@Produces("application/json")
public String postMahasiswaByID(@PathParam("id")String nim, String data)
{
    JSONObject jo = new JSONObject();
    jo.put("id", nim);
    jo.put("nama", "Budi");
    jo.put("message", data);
    return(jo.toJSONString());
}

@Path("/mahasiswaQuery")
@GET
@Produces("application/json")
public String getMahasiswaQuery(@QueryParam("nim")String nim, @QueryParam("nama") String nama)
{
    JSONObject jo = new JSONObject();
    jo.put("nim", nim);
    jo.put("nama", nama);
    jo.put("method", "GET");
    return(jo.toJSONString());
}

@Path("/mahasiswaQuery")
@POST
@Produces("application/json")
public String postMahasiswaQuery(@QueryParam("nim")String nim, @QueryParam("nama") String nama)
{
    JSONObject jo = new JSONObject();
    jo.put("nim", nim);
    jo.put("nama", nama);
    jo.put("method", "Post");
    return(jo.toJSONString());
}   
}

When i generate Restclient 


import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;

/**
 *
 * @author Vidhyadharan Deivamani (it.vidhyadharan@gmail.com)
 */
public class RestClient {

    static class RestWs_JerseyClient {

        private WebTarget webTarget;
        private Client client;
        private static final String BASE_URI = "http://localhost:9999/madrasjug";

        public RestWs_JerseyClient() {
            client = javax.ws.rs.client.ClientBuilder.newClient();
            webTarget = client.target(BASE_URI).path("restws");
        }

        public String putJson(Object requestEntity) throws ClientErrorException {
            return webTarget.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
        }

        public String getMahasiswaByID(String id) throws ClientErrorException {
            WebTarget resource = webTarget;
            resource = resource.path(java.text.MessageFormat.format("mahasiswaData/{0}", new Object[]{id}));
            return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
        }

        public String postMahasiswaQuery() throws ClientErrorException {
            return webTarget.path("mahasiswaQuery").request().post(null, String.class);
        }

        public String getMahasiswaQuery(String nim, String nama) throws ClientErrorException {
            WebTarget resource = webTarget;
            if (nim != null) {
                resource = resource.queryParam("nim", nim);
            }
            if (nama != null) {
                resource = resource.queryParam("nama", nama);
            }
            resource = resource.path("mahasiswaQuery");
            return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
        }

        public String postMahasiswaByID(Object requestEntity, String id) throws ClientErrorException {
            return webTarget.path(java.text.MessageFormat.format("mahasiswaData/{0}", new Object[]{id})).request(javax.ws.rs.core.MediaType.TEXT_PLAIN).post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.TEXT_PLAIN), String.class);
        }

        public String getJson() throws ClientErrorException {
            WebTarget resource = webTarget;
            return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(String.class);
        }

        public void close() {
            client.close();
        }
    }
    
}


The content-type for putJson  request supposed to be javax.ws.rs.core.MediaType.TEXT_PLAIN but it is generated 
javax.ws.rs.core.MediaType.APPLICATION_JSON

Hence it is throwing exception HTTP 406 Not Acceptable


http://stackoverflow.com/questions/36288230/how-to-make-my-netbeans-java-rest-client-access-post-method
Comment 1 rpbostock909 2018-02-08 21:55:28 UTC
Similarly this has failed for me. I was using the following tutorial resource, and did an autogenerate of a restful client to test netbeans functionality.

https://github.com/koushikkothagal/messenger

Pointing it at the message resource it failed to create the get functions correctly. Example is shown below for getMessage, but getMessages did the same.

It created:
public <T> T getMessage(Class<T> responseType, String messageId) throws ClientErrorException {
            WebTarget resource = webTarget;
            resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{messageId}));
            return resource.get(responseType);
        }

Instead of:
public <T> T getMessage(Class<T> responseType, String messageId) throws ClientErrorException {
            WebTarget resource = webTarget;
            resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{messageId}));
            return resource.request(MediaType.APPLICATION_JSON).get(responseType);
        }