Skip to main content



      Home
Home » Eclipse Projects » Eclipse Scout » Cannot create/update REST resource(REST POST & PUT commands not working in my code)
Cannot create/update REST resource [message #1854199] Fri, 05 August 2022 10:49 Go to next message
Eclipse UserFriend
Hi there everyone,

I'm having problems creating/updating REST resources on a remote server. The methods I'm using are shown below:
  // POST
  public Response post(ContactEntityDo entity) {
    Map<String, String> params = Map.of("schema", SCHEMA_NAME);
    JSONObject jsParams = new JSONObject(params);

    WebTarget target = helper().target(RESOURCE_PATH).queryParam("params",
        UriComponent.encode(jsParams.toString(), UriComponent.Type.QUERY_PARAM_SPACE_ENCODED));
    return target.request().accept(MediaType.APPLICATION_JSON).post(Entity.json(entity),
        Response.class);
  }

  // PUT
  public Response put(ContactEntityDo entity) {
    Map<String, String> params = Map.of("schema", SCHEMA_NAME);
    JSONObject jsParams = new JSONObject(params);

    WebTarget target = helper().target(RESOURCE_PATH).queryParam("params",
        UriComponent.encode(jsParams.toString(), UriComponent.Type.QUERY_PARAM_SPACE_ENCODED));
    return target.request().accept(MediaType.APPLICATION_JSON).put(Entity.json(entity),
        Response.class);
  }


Here is the stacktrace
java.lang.NullPointerException: null
	at org.eclipse.scout.apps.ygclient.client.rest.common.ResponseRestClientHelper.transformException(ResponseRestClientHelper.java:34)
	at org.eclipse.scout.rt.rest.client.proxy.RestClientProxyFactory$RestProxyInvocationHandler.transformException(RestClientProxyFactory.java:256)
	at org.eclipse.scout.rt.rest.client.proxy.RestClientProxyFactory$RestProxyInvocationHandler.invoke(RestClientProxyFactory.java:206)
	at com.sun.proxy.$Proxy58.post(Unknown Source)
	at org.eclipse.scout.apps.ygclient.client.rest.collaborator.contact.ContactRestResourceClient.post(ContactRestResourceClient.java:61)
	at org.eclipse.scout.apps.ygclient.client.collaborator.contact.ContactForm$MainBox$GeneralBox$SaveButton.execClickAction(ContactForm.java:352)
	at org.eclipse.scout.rt.client.ui.form.fields.button.AbstractButton$LocalButtonExtension.execClickAction(AbstractButton.java:515)
	at org.eclipse.scout.rt.client.extension.ui.form.fields.button.ButtonChains$ButtonClickActionChain$1.callMethod(ButtonChains.java:59)
	at org.eclipse.scout.rt.client.extension.ui.form.fields.button.ButtonChains$ButtonClickActionChain$1.callMethod(ButtonChains.java:1)
	at org.eclipse.scout.rt.shared.extension.AbstractExtensionChain.callChain(AbstractExtensionChain.java:118)
	at org.eclipse.scout.rt.client.extension.ui.form.fields.button.ButtonChains$ButtonClickActionChain.execClickAction(ButtonChains.java:62)
	at org.eclipse.scout.rt.client.ui.form.fields.button.AbstractButton.interceptClickAction(AbstractButton.java:499)
	at org.eclipse.scout.rt.client.ui.form.fields.button.AbstractButton.doClick(AbstractButton.java:341)
	at org.eclipse.scout.rt.client.ui.form.fields.button.AbstractButton$P_UIFacade.fireButtonClickFromUI(AbstractButton.java:469)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.eclipse.scout.rt.client.ModelContextProxy.lambda$1(ModelContextProxy.java:49)
	at org.eclipse.scout.rt.platform.chain.callable.CallableChain$Chain.continueChain(CallableChain.java:227)
	at org.eclipse.scout.rt.platform.transaction.TransactionProcessor.runTxMandatory(TransactionProcessor.java:156)
	at org.eclipse.scout.rt.platform.transaction.TransactionProcessor.runTxRequired(TransactionProcessor.java:139)
	at org.eclipse.scout.rt.platform.transaction.TransactionProcessor.intercept(TransactionProcessor.java:78)
	at org.eclipse.scout.rt.platform.chain.callable.CallableChain$Chain.continueChain(CallableChain.java:222)
	at org.eclipse.scout.rt.platform.chain.callable.CallableChain.call(CallableChain.java:170)
	at org.eclipse.scout.rt.platform.context.RunContext.call(RunContext.java:158)
	at org.eclipse.scout.rt.client.ModelContextProxy.lambda$0(ModelContextProxy.java:49)
	at com.sun.proxy.$Proxy62.fireButtonClickFromUI(Unknown Source)
	at org.eclipse.scout.rt.ui.html.json.form.fields.button.JsonButton.handleUiEvent(JsonButton.java:145)
	at org.eclipse.scout.rt.ui.html.json.JsonEventProcessor.processEvent(JsonEventProcessor.java:52)
	at org.eclipse.scout.rt.ui.html.json.JsonEventProcessor.processEvents(JsonEventProcessor.java:37)
       ...........


The problem seems to be from the return line in both of the methods. This is line 61 of my ContactRestResourceClient.java class
    return target.request().accept(MediaType.APPLICATION_JSON).post(Entity.json(entity),
        Response.class);


I've read Chapter 17 REST of the Scout Technical Guide, but I still cannot solve the problem. Can anyone please help me figure out what I'm doing wrong?

Cheers,

JD

[Updated on: Fri, 05 August 2022 11:27] by Moderator

Re: Cannot create/update REST resource [message #1854215 is a reply to message #1854199] Mon, 08 August 2022 04:56 Go to previous messageGo to next message
Eclipse UserFriend
According to the stack trace the NPE comes from your ResponseRestClientHelper. Could you post its code, specially around line 34?
Which nested exception do you receive from your backend?

Cheers, Paolo
Re: Cannot create/update REST resource [message #1854218 is a reply to message #1854215] Mon, 08 August 2022 06:30 Go to previous messageGo to next message
Eclipse UserFriend
Hi there Paolo,

Thanks for your reply. Here is the code of the ResponseRestClientHelper.java class

@Bean
public class ResponseRestClientHelper extends AbstractRestClientHelper {

  /*
   * returns the path to the REST server e.g "http://localhost:28088/ygapi/service/v2/root/"
   */
  @Override
  protected String getBaseUri() {
    return RestEngineSingleton.getBaseUri();
  }

  protected String getSchema() {
    return RestEngineSingleton.getSchema();
  }

  /*
   * Customize the ErrorResponse class and the ErrorDo
   * 
   * See p143 of the Technical Guide
   */
  @Override
  protected RuntimeException transformException(RuntimeException e, Response response) {
    if (response != null && response.hasEntity()) {
      ErrorDo error = response.readEntity(ErrorResponse.class).getError();
      throw new VetoException(error.getMessage()).withTitle(error.getTitle());
    }
    return e;
  }
}


Line 34 of the file is
      throw new VetoException(error.getMessage()).withTitle(error.getTitle());


The exception message is shown in the attached image. It's in French, but tit says an internal processing error occurred with error code N20.

Cheers,

JD
Re: Cannot create/update REST resource [message #1854221 is a reply to message #1854218] Mon, 08 August 2022 07:30 Go to previous messageGo to next message
Eclipse UserFriend
...
ErrorDo error = response.readEntity(ErrorResponse.class).getError();
throw new VetoException(error.getMessage()).withTitle(error.getTitle());


Apparently, the "error" attribute of the ErrorResponse object is null. You might want to add a null-check here.

Quote:

The exception message is shown in the attached image. It's in French, but tit says an internal processing error occurred with error code N20.


What you are seeing is the NullPointerException caused by the error transformation, not the original error. Try setting a breakpoint and inspecting the content of the ErrorResponse object.

Regards,
Beat
Re: Cannot create/update REST resource [message #1854222 is a reply to message #1854218] Mon, 08 August 2022 07:35 Go to previous messageGo to next message
Eclipse UserFriend
Hi JD,

OK I think the problem is, that your exception transformer expects an ErrorDo to be available in the response in case of an error. This assumption is only valid, if you use the Scout REST support on both sides of the REST communication (client and server). In this case on server side any occurred errors are reported back to the client as ErrorDo (see org.eclipse.scout.rt.rest.exception.DefaultExceptionMapper#createResponse)

In your case you invoke an arbitrary REST resource, which will return its own style of errors. For this use case, you rather should use the BasicRestClientExceptionTransformer to handle exceptions or implement an own exception transformer if your invoked API returns back error details as JSON object.

Simple example:

  @Override
  protected RuntimeException transformException(RuntimeException e, Response response) {
    return BEANS.get(BasicRestClientExceptionTransformer.class).transform(e, response);
  }


If you want to implement a custom exception handler based on an error entity returned by the API use this class as a starting point:
org.eclipse.scout.rt.rest.client.proxy.AbstractEntityRestClientExceptionTransformer

Cheers, Paolo
Re: Cannot create/update REST resource [message #1854322 is a reply to message #1854222] Sat, 13 August 2022 11:23 Go to previous messageGo to next message
Eclipse UserFriend
Thank you very much @Beat and @Paolo. Thanks to your contributions, I've been able to correct the problem and I'm now able to update the backend Contact table behind the REST Server.

Much appreciated.

Cheers,

JD
Re: Cannot create/update REST resource [message #1854334 is a reply to message #1854322] Mon, 15 August 2022 02:05 Go to previous message
Eclipse UserFriend
Good to hear, thanks for your post!
Previous Topic:Programmatically defined saved settings for table
Next Topic:How to not write RAW SQL in scout application
Goto Forum:
  


Current Time: Sun Apr 20 22:49:00 EDT 2025

Powered by FUDForum. Page generated in 0.04896 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top