How to have a @PATCH annotation for JAX-RS?

前端 未结 5 657
渐次进展
渐次进展 2021-01-30 19:51

JAX-RS has annotations for HTTP verbs such as GET (@GET) and POST (@POST) but there is no @PATCH annotation. How

5条回答
  •  青春惊慌失措
    2021-01-30 20:30

    In Jersey this will work just fine, but when using Jersey Client to test your resource class, you will get exceptions:

    java.net.ProtocolException: Invalid HTTP method: PATCH
    

    There is a workaround for this, by setting client property

    HttpUrlConnectorProvider.SET_METHOD_WORKAROUND 
    

    But wait, then you will end up with the following exception:

    javax.ws.rs.ProcessingException: java.net.ProtocolException: HTTP method PATCH doesn't support output
    

    So there is no other way than changing to the Apache HTTP client library, using Jersey version 2.10, its easy to configure to use Apache HTTP client, you only need to override the client config method in your test class that extends JerseyTest.

    @Override
    protected void configureClient(ClientConfig config) {
      config.register(CustomJacksonJsonProvider.class);
      ConnectorProvider connectorProvider = new ApacheConnectorProvider();
      config.connectorProvider(connectorProvider);
    }
    

    And you need to also add another Maven dependency, jersey-apache-connector and jersey-test-framework-provider-external, see Jersey doc

提交回复
热议问题