AngularJS Client, JAX-RS Jersey Rest Service - application/json client request throwing 'Error parsing media type'

一曲冷凌霜 提交于 2019-12-03 10:04:29

Your Content-Type header is invalid (see spec 14.17 Content-Type). Based on you JAX-RS resource you should use application/json as Content-Type (without application/x-www-form-urlencoded):

$http({
  method: 'POST',
  data: $scope.Patient,
  url:'/ManagePatient/AddPatient',
  headers: {'Content-Type':'application/json'}
});

EDIT 1:

If you want to see requests that are coming to your server you can register LoggingFilter and it will show you some useful information. You can turn it on in:

web.xml (add it to the JAX-RS servlet definition):

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>

Application extension:

public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>() {{
            // Add your resources.
            add(HelloWorldResource.class);

            // Add LoggingFilter.
            add(LoggingFilter.class);
        }};
    }
}

ResourceConfig instance (demonstrating also outputting the entity here):

public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Resources - add your package name here to enable package scanning.
        packages(...);

        // Enable LoggingFilter & output entity.     
        registerInstances(new LoggingFilter(Logger.getLogger(MyApplication.class.getName()), true));
    }
}

EDIT 2:

Jersey 2.x does not support application/json media type out of the box - you need to add one of the JSON modules to your classpath (see JSON section in users guide), for example:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.0</version>
</dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!