HTTP 406 Not Acceptable in Spring, Jackson, Json

■ 참고
spring mvc 406에러 – Not Acceptable
http://knight76.tistory.com/entry/spring-mvc-406-%EC%97%90%EB%9F%AC-Not-Acceptable
Spring Boot Application: No converter found for return value of type
https://stackoverflow.com/questions/33832735/spring-boot-application-no-converter-found-for-return-value-of-type/33896080#33896080
 
 
 

■ pom.xml에 jackson lib추가

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

 
 
 

■ spring 설정파일에 annotation-driven 추가

<mvc:annotation-driven />

 
 
 

■ json으로 표현 못하는 Class를 json으로 만들때 발생 , 적당하게 map, array, list로 표현할 수 있도록 변환
 
 
 

■ 나의 경우는 위의 사항을 전부 지켰지만 오류가 해결 되지 않았다.

@Controller
@RequestMapping("jsonsample")
public class JsonSampleController {
       @RequestMapping(
                     value  = "sample.jsonf",
                     method        = RequestMethod.GET,
                     produces=MediaType.APPLICATION_JSON_VALUE)
       @ResponseBody
       public List<SampleVO>  listSample(
                     @ModelAttribute Params params) throws Exception {

              List<SampleVO> sampleList = new ArrayList<SampleVO>();
              SampleVO sampleVO = new SampleVO();
              sampleVO.setId("id01");
              sampleVO.setName("name01");
              sampleList.add(sampleVO);
              return sampleList;
       }
}

 
 
 

■ 위의 코드에서 produces=MediaType.APPLICATION_JSON_VALUE 부분을 제거해보았더니 다음과 같은 에러발생.
No converter found for return value of type: class java.util.ArrayList] with root cause
 
 
 

■ stackoverflow에서 해결책을 찾았는데 설정은 아래와 같다.
– pom.xml, jackson버전은 동일하게 맞춰야한다. 1.x이면 1.x로, 2.x면 2.x로 통일한다.

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

 
 
 

– spring 설정파일

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

 
 
 

– Spring Controller

@Controller
@RequestMapping("jsonsample")
public class JsonSampleController {

       @RequestMapping(
                     value  = "sample.jsonf",
                     method        = RequestMethod.GET,
                     produces=MediaType.APPLICATION_JSON_VALUE)
       @ResponseBody
       public List<SampleVO>  listSample(
                     @ModelAttribute Params params) throws Exception {

              List<SampleVO> sampleList = new ArrayList<SampleVO>();
              SampleVO sampleVO = new SampleVO();
              sampleVO.setId("id01");
              sampleVO.setName("name01");

              sampleList.add(sampleVO);
              return sampleList;
       }
}

 
 
 

– 참고1. SampleVO

public class SampleVO {

       private String id;
       private String name;
       private int age;
       private int level;

       public int getLevel() {
              return level;
       }
       public void setLevel(int level) {
              this.level = level;
       }
       public String getId() {
              return id;
       }
       public void setId(String id) {
              this.id = id;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       public int getAge() {
              return age;
       }
       public void setAge(int age) {
              this.age = age;
       }
}

 
 
 

– 참고2. angularjs client

<!DOCTYPE html>
<html ng-app="myApp">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
<head>
<title></title>
<script src="/js/angularjs/1.5.6/angular.js"></script>
<script src='/js/jquery/jquery-1.11.0.min.js'></script>
<script>
var myApp = angular.module("myApp",[]);
myApp.controller("myAppCtrl", function($scope,$http){
       $scope.result = "";
       $scope.start = function(event) {
              $http({
                     method : 'GET',
                     cache  : false,
                     url           : '/jsonsample/sample.jsonf'
              })
              .success(function(response){
                     console.log(response);
                     $scope.result = response;
              });
       }
});
</script>
</head>
<body ng-controller="myAppCtrl">
       <h1>
              AngularJS Built-in Service $http<br/>
       </h1>
       <button ng-click="start()">$http</button>
       <hr/>
       my location <div>{{result}}</div>
</body>
</html>

 
 
 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다