다음 링크 (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 코드 잠시 동안, 그리고 이것에 대한 해결책을 찾을 수 없었습니다.
컨트롤러 코드를 표시 할 수 있습니까? –
스프링 (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
해당 요청을 처리하는 컨트롤러 게시 –