14

have following details in my pom.xml

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>

application-config.xml:

<context:component-scan base-package="com.test" />
<mvc:annotation-driven />
<!-- <mvc:default-servlet-handler /> -->
<mvc:resources mapping="/resources/**" location="/resources/" />

JSP page:

 <form:form method="POST" action="/QuickBooks-UX/syncAccounts">
        <input type="submit" value="Sync Account"/>
 </form:form>

Controller:

@Controller
@RequestMapping("/")
public class QuickBooksController {
    @RequestMapping(value = "/quickBooks", method = RequestMethod.GET)
    public String qucikBooks(ModelMap model) {
        logger.info("Welcome to QuickBooks controller");
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "quickBooks";
    }
    @RequestMapping(value ="/syncAccounts", method = RequestMethod.POST)
    public @ResponseBody List<SyncData> syncAccounts(@ModelAttribute("syncData")SyncData syncData, ModelMap model, BindingResult result) {
          List<SyncData> syncDataList = new ArrayList<SyncData>();
          try {

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet getRequest = new HttpGet(
                    "http://localhost:8292/qbsyncdata/getAccounts");
                getRequest.addHeader("accept", "application/json");

                HttpResponse response = httpClient.execute(getRequest);
                if (response.getStatusLine().getStatusCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                       + response.getStatusLine().getStatusCode());
                }
                BufferedReader br = new BufferedReader(
                                 new InputStreamReader((response.getEntity().getContent())));                   
                while ((output = br.readLine()) != null) {
                    JSONParser jsonParser = new JSONParser();
                    JSONArray jsonArray = (JSONArray)jsonParser.parse(output);
                    for (Object object : jsonArray) {
                        JSONObject jsonObject = (JSONObject)object;
                        syncData = new SyncData();
                        syncData.setAccountName(jsonObject.get("accountName")==null?"":jsonObject.get("accountName").toString());
                        syncData.setAccountType(jsonObject.get("accountType")==null?"":jsonObject.get("accountType").toString());
                        syncData.setAccountSubType(jsonObject.get("accountSubType")==null?"":jsonObject.get("accountSubType").toString());
                        syncData.setActive(jsonObject.get("active")==null?"":jsonObject.get("active").toString());
                        syncDataList.add(syncData);
                    }                   
                    model.addAttribute("syncData", output);
                }
                httpClient.getConnectionManager().shutdown();
              } catch (Exception e) {
                e.printStackTrace();
              } 
            }
         return syncDataList;
    }
}

I am invoking my url as :

http://lt-50k7sy1:8080/QuickBooks-UX/quickBooks

After clicking the button, which returns the url as http://lt-50k7sy1:8080/QuickBooks-UX/syncAccounts this returns 406 and description is:

the resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers

I have followed this Link, but no results.

Community
  • 1
  • 1
AKB
  • 5,918
  • 10
  • 53
  • 90
  • 2
    Faced similar problem,and followed http://stackoverflow.com/questions/26416248/srping-4-1-1-release-and-responsebody-return-http-406 – dReAmEr Oct 28 '14 at 18:24

6 Answers6

44

Add following jar to your pom.xml file which is required for Spring 4.1.*

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.1</version>
</dependency>
TechFind
  • 3,696
  • 18
  • 47
  • 62
  • Thanks, it is helpful (I used version 2.4.3). Also found http://xpadro.blogspot.ca/2014/01/migrating-spring-mvc-restful-web.html migration guide helpful. – rodche Nov 11 '14 at 19:23
6

Get rid of this issues by adding @EnableWebMvc in Controller class.

@Controller
@RequestMapping("/API/course")
@EnableWebMvc
public class CourseController {
 @Autowired
 private com.item.DAO.CourseRepository courseRepository;

 @ResponseStatus(value=HttpStatus.OK)
 @RequestMapping(method=RequestMethod.GET)
 public @ResponseBody List<Course> getListOfCourse(){
    List<Course> courses = courseRepository.getListOfCourse();
    return courses ;
}

or add following line in xml configuration file if XML configuration is being used in your project.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<bean>

and add following dependencies in your pom.xml file:-

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.1</version>
    </dependency>
Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44
  • 1
    Just adding dependencies didn't work for me. @EnableWebMvc was missing here. Thank you! – Eduardo Feb 07 '16 at 16:50
  • 1
    WOW @EnableMvcController fixed my problem. Thank you so much. But this is horrible, we're adding some "fixes"/"hacks" on top of a rotten Spring infrastructure, it really sucks. I had a proper RestController structure, and it wasn't working, only this "hack" helped. – gene b. Nov 22 '17 at 16:48
1

Thanks it solved my issue. I finally ended up with a pom.xml like this:

<!-- Json dependency -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1.1</version>
    </dependency>
    <!-- Json dependency -->
zipzapsam
  • 11
  • 1
1

Use below dependency in your pom working with spring 4.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.3</version>
</dependency>
Nirbhay Rana
  • 4,229
  • 2
  • 18
  • 4
1

Maybe all fields of your POJO need Getter and Setter.

I fixed it according to this issue: reference:Spring MVC - HttpMediaTypeNotAcceptableException

And 406 is not a useful message to fix the bug. You should debug into the codes and see what the Exception is on earth.

Community
  • 1
  • 1
Dai Kaixian
  • 1,045
  • 2
  • 14
  • 24
0

It worked for me with the below dependencies and @EnableWebMvc in my RestController, Please see that I just added jackson-databind dependency.

  <properties>
    <spring-version>4.2.1.RELEASE</spring-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring-version}</version>  
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring-version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.3</version>
    </dependency>
  </dependencies>
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34