만들어가는 세상

[SPRING] 406 ERROR 발생시 대처 방법 본문

IT/SPRING

[SPRING] 406 ERROR 발생시 대처 방법

윤재웅 2019. 11. 26. 18:01

[SPRING] 406 ERROR 발생시 대처 방법

step1 pom.xml 에서 dependencies 합니다.

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

contentType 설정

$.ajax({
    type: 'POST',
    url: vewMoreVar.apiUrl,
    contentType: 'application/json;charset=utf-8',
    data: JSON.stringify(dataObj),
    dataType: 'json',
    cache: false,
    async: false,
    success: function(result) {
        console.log(result);
    },
    error: function(request, status, error) {
        console.log("code:" + request.status + "\n" + "message:" + request.responseText + "\n" + "error:" + error);
    }
});

dispatcher-servlet.xml 설정

<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML" />
    <property name="cacheable" value="false" />
</bean>

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="true" />
    <property name="ignoreAcceptHeader" value="true" />
    <property name="useJaf" value="false" />
    <property name="defaultContentType" value="text/html" />

    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html" />
            <entry key="json" value="application/json" />
        </map>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="contentNegotiationManager" ref="contentNegotiationManager" />
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
                <property name="order" value="1"></property>
            </bean>
            <ref bean="thymeleafView" />
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
        </list>
    </property>
</bean>

TEST.JAVA

    @RequestMapping(value = "/more/directory/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<Map<String, Object>> moreDirectoryList(@RequestBody ViewMore viewMore) throws Exception {
        Map<String, Object> params = dictionaryController.getCategoryCode(viewMore.getCode());
        Paging paging = new Paging(viewMore.getPage(), PAGE_SIZE.DEFAULT);
        return dictionaryService.getDirectoryList(params, paging);
    }
Comments