Spring Boot 2.1.2 + POSTMAN + 415 Unsupported Media "application/json"

I know this question is answered multiple times but somehow I am not able to get past it. Also, I haven't found Unsupported Media "application/json" error.

EDIT: @RequestBody works when I select x-www-form-urlencoded in POSTMAN but not with raw -> application/json

I just want to create a record by posting data from POSTMAN.

Here is my code:

Spring Boot Version: 2.1.2

I read that jackson data bind is already there in 2.1.2 version of spring boot.

POSTMAN Request:
Body -> raw -> application/json

{
  "first_name": "ashutosh",
  "middle_name": "",
  "last_name": "pandey",
  "gender": "male",
  "address": "my address"
}

OR

{
  "firstName": "ashutosh",
  "middleName": "",
  "lastName": "pandey",
  "gender": "male",
  "address": "my address"
}

OR

{
  "practitioner": {
  "first_name": "ashutosh",
  "middle_name": "",
  "last_name": "pandey",
  "gender": "male",
  "address": "my address"
  }
}
POSTMAN Headers:
Content-Type: application/json
Accept: application/json 
Spring Boot Controller
@RestController
@RequestMapping("/practitioner")
public class PractitionerController{
@Autowired private CrudService<Practitioner> crudService;

/**
 * POST /create --> Create a new Practitioner and save it in the database.
 */
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> create(@RequestBody Practitioner practitioner) 
{
    crudService.save(practitioner);
    String practitionerId = String.valueOf(practitioner.getId());
    return ResponseEntity.ok().body(practitionerId);
}
}
My Entity
package auto.nicu.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;

@Entity
@Table(name="practitioners")
public class Practitioner implements Serializable{
private static final long serialVersionUID = 6017382414498150143L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id") private Integer id;

@Column(name="gender", length=256) private String gender;
@Column(name="address", length=256) private String address;
@Column(name="last_name", length=256) private String lastName;
@Column(name="first_name", length=256) private String firstName;
@Column(name="middle_name", length=256) private String middleName;

@OneToMany(mappedBy="practitioner") Set<PractitionerAction> practitionerActions;

public Practitioner() {}

/************* Getters *******************/

public Integer getId() {  return id; }  

public String getGender() {  return gender; }
public String getAddress() {  return address; }
public String getLastName() {  return lastName; }
public String getFirstName() {  return firstName; }
public String getMiddleName() {  return middleName; }

public Set<PractitionerAction> getPractitionerActions() {  return practitionerActions; }

/************* Setters *******************/

public void setId(Integer id) { this.id = id; }

public void setGender(String gender) { this.gender = gender; }
public void setAddress(String address) { this.address = address; }
public void setLastName(String lastName) { this.lastName = lastName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public void setMiddleName(String middleName) { this.middleName = middleName; }

public void setPractitionerActions(Set<PractitionerAction> practitionerActions) { this.practitionerActions = practitionerActions; }
}

Whatever I do, I always get:

{"timestamp":"2019-04-01T11:46:15.420+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/json;charset=UTF-8' not supported","trace":"org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:224)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)\n\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter....

I tried removing @RequestBody, added consumes="application/json" or consumes=MediaType.APPLICATION_JSON_VALUE, the error is gone but I get null values in database.

I tried to create a simple html form and submitted data with jquery, but same error :(

  <script type="text/javascript">
$(function(){
    $("#btn").click(function(){
        $.ajax({
            url: 'http://localhost:8080/practitioner/',
            method: 'post',
            data: $("#form").serialize(),
            dataType: 'json',
            headers: {
                'Authorization':'Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6ImFkbWluIiwiaWF0IjoxNTU0MTcxNzA0fQ.o7AYTb8sE0iT5YMLndPUnkvdK5-sWG3u26gfK9z56ls',
                'Content-Type':'application/json'
            },
            success: function(result){
                console.log(result);
            }
        });
    });
});
</script>

Is it a misleading error?

Here is the error stack trace:

{"timestamp":"2019-04-02T02:29:27.522+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/json;charset=UTF-8' not supported","trace":"org.springframework.web.HttpMediaTypeNotSupportedException: 
Content type 'application/json;charset=UTF-8' not supported\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:224)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)\n\tat     
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166)\n\tat 
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n\tat 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)\n\tat org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)\n\tat org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat 
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat 
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat 
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)\n\tat 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)\n\tat 
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat 
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)\n\tat org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)\n\tat 
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)\n\tat org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n\tat 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Thread.java:748)\n","path":"/practitioner/"}

Change @PostMapping(headers="Accept=application/json") for @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)

Also in Postman use request to user CammelCase instead of _

For Example

user firstName instead of first_name, OR

you can set JsonProperties to Practitioner to receive the properties as you want.

Spring Boot Reference Documentation, Browse folder. Downloads file_download. Spring Boot Dependencies Spring Boot Dependencies. org.springframework.boot:spring-boot-dependencies 2.1.2. On behalf of the team and everyone who has contributed, I’m happy to announce that Spring Boot 2.1.2 has been released and is is now available from repo.spring.io and Maven Central. This release includes over 60 fixes, improvements and dependency upgrades. Thanks to all those who have contributed with issue reports and pull requests.

There are many problems here. I would try to:

  1. Remove "Accept: application/json" from Postman. This header means the client only accepts "application/json" as response type
  2. Remove (headers="Accept=application/json") in @PostMapping
  3. Remove @ResponseBody as it is not needed in @RestController

What is the content of your Practitioner class? The path of your Postman request is /practitioner ?

org.springframework.boot � spring-boot � 2.1.2 , I'm migrating from java-oracle-8 to java11 based on adopteOpenJdk release. my app it based on spring boot 2.1.2.REAEASE. Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.

Check out some possible reasons here: http://javahonk.com/415-unsupported-media-type/

As of Spring Boot 2.1, First released in mid-2014. Spring Boot has been through a lot of development and improvement. Its version 2.0 is today getting ready for release� Spring Boot Version: 2.1.2. I read that jackson data bind is already there in 2.1.2 version of spring boot. POSTMAN Request: Spring Boot Controller

org.springframework.boot : spring-boot-dependencies : 2.1.2 , Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that can you can "just run". We take an opinionated view of the� In Spring Boot 2, Hikari is the default DataSource implementation. This is what's changed from Spring Boot 1.x: the dependency to Hikari is now automatically included in spring-boot-starter-data-jpa; the discovery algorithm that automatically determines a DataSource implementation now prefers Hikari over TomcatJDBC (see the reference manual).

Central Repository: org/springframework/boot/spring-boot/2.1.2 , Maven. To get started with Spring Boot 2.2. in your application update to the new version of the starter parent. I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: >> CHECK OUT THE COURSE If you have some solid experience in the Java ecosystem (6+ years), and you're interested in sharing that experience with the community (and getting paid for your work of course), we’ve just opened up a new position

adoptOpenJdk 11.0.5, spring boot 2.1.2.RELEASE, On behalf of the community, I’d like to announce the availability of the Spring Vault service release 2.1.2 available from Maven Central. This release ships with bug fixes and dependency version updates. Spring Vault 2.1.2 is going to be picked up by Spring Cloud Vault 2.1.2 with the Spring Cloud Greenwich SR1 release in the next days.

Comments
  • Updated code, removed headers, added consumes, using camel case now, added stack trace, still error :(
  • Tried but same problem :( , added Practitioner class above
  • You should use the first body (without "practitioner") in Postman. Use camel case for names (firstName, middleName, lastName) as the default configuration of objectMapper uses getter / setter for serialization / deserialization Can you post the exception stacktrace as well?
  • Hi @htn, added stack trace
  • Yes, the path is /practitioner
  • You should post the stack trace from the server side, it is easier to read. If the error is gone, use debug in your controller method to understand why it does not work.