0

I have a problem. I try so send http post to my controller with angularjs.

@RequestMapping(value = "/books/manage", method = RequestMethod.POST)
@ResponseBody
public void manageBooks(@RequestBody final BooksDTO dto)
        throws SystemException, IOException {
    System.out.println("DTO WAS SEND!");
    }
}

Here Angularjs

$http.post($scope.BooksUrl, {
                        'title':Title,
                        'booksUrl':Url,
                        'number':Number
                }).error(function (response) {
                    // error message
                }).then(function(){
                    // success message
                });

The header is

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Connection:keep-alive
Content-Length:72
Content-Type:application/json;charset=UTF-8

But It catchs "HTTP - 415 status. The server refused this request because the request entity is in a format not supported by the requested resource for the requested method." How I can resolve it?

Responce headers

Content-Length:1048
Content-Type:text/html;charset=utf-8
Date:Wed, 21 Oct 2015 06:00:39 GMT
Server:Apache-Coyote/1.1
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1

pom.xml

 <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>portal-service</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>util-bridges</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>util-taglib</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>util-java</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.portlet</groupId>
        <artifactId>portlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!--Spring dependencies-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"   xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:component-scan base-package="com.example.books.**"/>
<tx:annotation-driven/>

Cœur
  • 37,241
  • 25
  • 195
  • 267
jahra
  • 1,173
  • 1
  • 16
  • 41

4 Answers4

1

try using the below code in your controller

@RequestMapping(value = "/books/manage", method = RequestMethod.POST,consumes="application/json")
gandharv garg
  • 1,781
  • 12
  • 17
1

This could also be due to missing message converter. Try to register one (Jackson in your case). The java object needs to be converted to JSON, this is done by JSON message converter. If if use @EnableWebMVC or mvc:annotation-driven tag (in case of xml config) and jackson is added to your classpath then MappingJacksonHttpMessageConverter is implicitly added. See if your pom has Jackson dependency else add following:

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.4.6</version>
</dependency>
cpd214
  • 605
  • 7
  • 11
0

I figured out the solution. The problem was in servlet xml:

<tx:annotation-driven/>

should be replaced with

<mvc:annotation-driven/>

I don't know what's the difference, but it works (Can someone explain?) Thank you all for answer.

jahra
  • 1,173
  • 1
  • 16
  • 41
0

I don't know what's the difference, but it works (Can someone explain?) Thank you all for answer.

tx:annotation-driven - used to enable transaction annotations like @Transactional

mvc:annotation-driven - used to enable Spring MVC annotations like @Controller

Here some similar question What's the difference between <mvc:annotation-driven /> and <context:annotation-config /> in servlet?

Community
  • 1
  • 1
pokemzok
  • 1,659
  • 1
  • 19
  • 29