2017-02-13 3 views
-1

간단한 스프링 웹 애플리케이션을 만들었습니다. @RequestMapping을 가진이 간단한 @Controller를 가지고 있지만 URL을 실행할 수 없습니다.간단한 스프링 웹 서비스에 어떤 문제가 있습니까?

http://localhost:8080/labutil/all 

무엇이 잘못 되었나요?

package com.mycompany.ion.labutil.controller; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.nokia.ion.labutil.service.LabService; 

@Controller 
public class LabController { 

    @Autowired 
    private LabService labService; 

    @RequestMapping(value = "/all", method = RequestMethod.GET) 
    public String getAll() throws Exception { 
     List<String> list = labService.getAll(); 

     // build fake little json formatted data 
     StringBuffer sb = new StringBuffer("{"); 
     for (String s : list) { 
      sb.append("{ "+s+" }, "); 
     } 
     sb.append("}"); 

     return sb.toString(); 
    } 


} 
+2

또한 http : // localhost : 8080/all을 시도 할 수 있습니까? 그것은 나에게 URL 문제처럼 보인다. – Kael53

+0

application-context.xml 파일을 게시 해주십시오. – SachinSarawgi

+0

컨트롤러에서/labutil을 설정하지 않습니다. @ Kael53 제안이 작동해야합니다 (URL에 앱 컨텍스트 추가) – Alfabravo

답변

1

컨트롤러를 @RestController로 주석 처리하거나 @ResponseBody 주석을 메서드에 추가해야합니다. 이 방법으로 Spring에게이 메소드가 객체를 HTTP body response로 반환한다고 말하고 있습니다. @RestController는 @Controller와 @ResponseBody 주석이 둘 다 주석 된 편리한 주석입니다.

Here이 주석을 사용해야하는 이유는 무엇입니까?

한편
@RequestMapping(value = "/all", method = RequestMethod.GET) 
@ResponseBody 
public String getAll() throws Exception { 
    List<String> list = labService.getAll(); 

    // build fake little json formatted data 
    StringBuffer sb = new StringBuffer("{"); 
    for (String s : list) { 
     sb.append("{ "+s+" }, "); 
    } 
    sb.append("}"); 

    return sb.toString(); 
} 

, 당신은 객체가 아닌 구문 분석 문자열 JSON을 반환 잭슨 또는 GSON 같은 몇 가지 JSON 라이브러리를 추가하고 각각의 라이브러리보기 구현보기를 구성해야합니다.