2013-11-27 4 views
0

다음 링크 (localhost : 8080/test/greeting)를 열면 성공적으로 실행되는 스프링 응용 프로그램을 만들었습니다. 서버가 완전히 배포되었지만 링크를 열면 greeting이라는 파일이 다운로드되고 json이 내부에 통합됩니다. Internet Explorer에서 링크를 열려고 시도했는데 json이 화면에 직접 나타나는 것으로 생각했습니다. 그렇지 않은가요? 여기 봄 휴식 json 출력

는 디스패처 서블릿입니다 :

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

    <context:component-scan base-package="test" /> 
    <mvc:annotation-driven /> 
</beans> 

그리고 여기의 web.xml입니다 :

package test; 

import java.util.concurrent.atomic.AtomicLong; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class GreetingController { 

    private static final String template = "Hello, %s!"; 
    private final AtomicLong counter = new AtomicLong(); 

    @RequestMapping("/greeting") 
    public @ResponseBody Greeting greeting(
      @RequestParam(value="name", required=false, defaultValue="World") String name) { 
     return new Greeting(counter.incrementAndGet(), 
          String.format(template, name)); 
    } 
} 

내가 찾던 :

여기
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

가 GreetingController 코드 잠시 동안, 그리고 이것에 대한 해결책을 찾을 수 없었습니다.

+0

컨트롤러 코드를 표시 할 수 있습니까? –

+1

스프링 (mvc)이 반환하는 응답 Content-Type이 올바르지 않다고 생각합니다. "application/json"이어야합니다. "Spring mvc content-type"을 검색하십시오. [이 오버플로 Q/A] (http://stackoverflow.com/questions/8951534/spring-mvc-3-return-content-type-text-plain)가 도움이 될 수 있습니다. 서블릿에'@ResponseBody'라는 주석을 달고'response.setContentType'을 사용하는 것이 좋습니다. – erny

+0

해당 요청을 처리하는 컨트롤러 게시 –

답변

0

인터넷 익스플로러 일 뿐이라서 다른 브라우저에서도 잘 작동합니다.

1

응답에 올바른 콘텐츠 형식이 설정되어 있지 않은 것으로 보입니다. 기본 응답 컨텐트 유형과 변환기를 JSON으로 설정하려면 반환 정의의 @ResponseBody 주석 이외에 스프링 컨텍스트 파일에 다음 bean 정의를 추가 할 수 있습니다.

<bean id="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <list> 
       <ref bean="jacksonMessageConverter" /> 
      </list> 
     </property> 
</bean> 

은 또한 실제 자바 < => JSON 변환을 처리하는 클래스 경로에 잭슨을 추가해야합니다. 기본적으로으로 원하는 경우 모든 요청 매핑 정의 방법에 대해 JSON으로 콘텐츠 유형을 수동으로 설정하는 데 필요한 상용구 코드를 방지 할 수 있습니다.