1

Hello guys I'm pretty desperate right now. I tried all the solutions and nothing works.

This is situation.

I'm trying to make a POST request to create a new entity. Everything worked when I had a table which didn't have any relationships and I sent JSON like this.

{
 "comment":"Something",
 "rating":4,
 "objekt_name":"restaurant"
}

What i'm trying to do now is to save entity with many to one relation and I send JSON like this.

{
 "comment":"Something",
 "rating":4,
 "objekt":{"id":4}
}

Whatever I do I always get 415 Unsupported Media Type. I tried all kind of solutions from @JsonIgnore to tricks with deserializations. Also tried with couple of solutions on stack.

Here is the code for controller and Entities.

@RequestMapping(value="/ocjenes", method = RequestMethod.POST)
@ResponseBody
public Ocjene create(@RequestBody Ocjene ocjene){
    ocjeneService.save(ocjene);
    return ocjene;
}

Objekt class.

@Entity
@Table(name="OBJEKT")
public class Objekt implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -1255230370407147756L;

    @Id
    @Column(name = "ID")
    @GeneratedValue
    private int id;

    @NotEmpty(message="Polje ne smije biti prazno!")
    @Column(name = "NAME")
    private String name;

    @ManyToOne
    @JoinColumn(name = "KATEGORIJA_ID")
    @JsonManagedReference
    @JsonIgnore
    private Kategorija kategorija;

    @OneToMany(mappedBy="objekt",fetch=FetchType.LAZY)
    @JsonBackReference
    private Set<Instanca> instance = new HashSet<Instanca>();

    @OneToMany(mappedBy="objekt",fetch=FetchType.LAZY)
    @JsonBackReference
    private Set<Ocjene> ocjene = new HashSet<Ocjene>();

    public Objekt() {}

    getters and setters
}

Ocjene class.

@Entity
@Table(name="OCJENE")
public class Ocjene implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -2724521893167717875L;


    @Id
    @Column(name = "ID")
    @GeneratedValue
    private int id;

    @Column(name="rating")
    private int rating;

    @Column(name="comment")
    private String comment;

    @Temporal(TemporalType.DATE)
    @Column(name="datum")
    private Date date;

    @ManyToOne
    @JoinColumn(name = "objekt_id")
    @JsonManagedReference
    private Objekt objekt;

    public Ocjene() {

    }

    getters and setters
}

Just to tell you that i don't send date because date is filled with trigger and it worked without date when there was no relation.

UPDATE Here is the log.

DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/restful/ocjenes'; against '/resources/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/restful/ocjenes'; against '/restful/**'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 1 of 7 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 2 of 7 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 3 of 7 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 4 of 7 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 5 of 7 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
DEBUG: org.springframework.security.web.authentication.AnonymousAuthenticationFilter - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 6 of 7 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 7 of 7 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /restful/ocjenes; Attributes: [ROLE_REMOTE]
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
DEBUG: org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.RoleVoter@ffafd3, returned: -1
DEBUG: org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.AuthenticatedVoter@707e93, returned: 0
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:206)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Calling Authentication entry point.
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/restful/ocjenes'; against '/resources/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/restful/ocjenes'; against '/restful/**'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 1 of 7 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 2 of 7 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 3 of 7 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
DEBUG: org.springframework.security.web.authentication.www.BasicAuthenticationFilter - Basic Authentication Authorization header found for user 'remote'
DEBUG: org.springframework.security.authentication.ProviderManager - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
DEBUG: org.springframework.security.web.authentication.www.BasicAuthenticationFilter - Authentication success: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@7e8e94c9: Principal: org.springframework.security.core.userdetails.User@c84af846: Username: remote; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_REMOTE; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_REMOTE
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 4 of 7 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 5 of 7 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
DEBUG: org.springframework.security.web.authentication.AnonymousAuthenticationFilter - SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@7e8e94c9: Principal: org.springframework.security.core.userdetails.User@c84af846: Username: remote; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_REMOTE; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_REMOTE'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 6 of 7 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes at position 7 of 7 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /restful/ocjenes; Attributes: [ROLE_REMOTE]
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@7e8e94c9: Principal: org.springframework.security.core.userdetails.User@c84af846: Username: remote; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_REMOTE; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_REMOTE
DEBUG: org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.RoleVoter@ffafd3, returned: 1
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Authorization successful
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - RunAsManager did not change Authentication object
DEBUG: org.springframework.security.web.FilterChainProxy - /restful/ocjenes reached end of additional filter chain; proceeding with original chain
DEBUG: org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter - Opening JPA EntityManager in OpenEntityManagerInViewFilter
DEBUG: org.hibernate.impl.SessionImpl - opened session at timestamp: 14070942418
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'restful' processing POST request for [/mobile/restful/ocjenes]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /ocjenes
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public hr.box.mobile.domain.Ocjene hr.box.mobile.restful.controller.RestController.create(hr.box.mobile.domain.Ocjene)]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'restController'
DEBUG: org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public hr.box.mobile.domain.Ocjene hr.box.mobile.restful.controller.RestController.create(hr.box.mobile.domain.Ocjene)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public hr.box.mobile.domain.Ocjene hr.box.mobile.restful.controller.RestController.create(hr.box.mobile.domain.Ocjene)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public hr.box.mobile.domain.Ocjene hr.box.mobile.restful.controller.RestController.create(hr.box.mobile.domain.Ocjene)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
DEBUG: org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'restful': assuming HandlerAdapter completed request handling
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG: org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter - Closing JPA EntityManager in OpenEntityManagerInViewFilter
DEBUG: org.springframework.orm.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
Diamond
  • 41
  • 4
  • Do you have Jackson on your class path? Can you post the content of the request you send, headers and body? – Sotirios Delimanolis Aug 03 '14 at 17:18
  • Yes Jackson is on the classpath. Everything worked when there was no relationship and than i changed database model design a little and when i am trying to send JSON with this part "objekt":{"id":5}. I'm sending request with advanced rest client chrome extension. You think maybe that is the problem? – Diamond Aug 03 '14 at 19:09
  • Turn your log level to DEBUG and see what gets logged. Post it here. – Sotirios Delimanolis Aug 03 '14 at 19:11
  • Here is the request. Status 415 Unsupported Media Type Show explanation Loading time: 404 Request headers User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36 Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo Content-Type: application/json Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: hr-HR,hr;q=0.8,en-US;q=0.6,en;q=0.4,sr;q=0.2,sl;q=0.2 Cookie: JSESSIONI – Diamond Aug 03 '14 at 19:13
  • Please edit your question and add the details there. – Sotirios Delimanolis Aug 03 '14 at 19:14

0 Answers0