2017-10-18 1 views
1

그래서 @RestController가 있으며 프런트 엔드 응용 프로그램의 스키마를 기반으로 XML을 반환하고 유효성을 검사하여 편집기에 표시하려고합니다. 오류를 json 형식으로 처리하여 js로 처리하고 표시하도록합니다.스프링 레스트 컨트롤러 @ExceptionHandler가 xml 콘텐츠 및 json 오류를 반환합니다.

@RestController 
public class UserController { 

    @RequestMapping(value = "/test", 
     method = RequestMethod.GET, 
     produces = MediaType.APPLICATION_XML_VALUE) 
    public ResponseEntity<String> throwException(
     @RequestParam(value = "flag", defaultValue = "false") Boolean flag 
    ) throws Exception { 
     if (flag) { 
      throw new Exception(); 
     } else { 
      return ResponseEntity.ok("<xml>hello</xml>"); 
     } 
    } 


    @ResponseStatus(HttpStatus.BAD_REQUEST) 
    @ExceptionHandler(Exception.class) 
    @ResponseBody 
    ServerError exceptionHandler(HttpServletRequest req, Exception ex) { 
     return new ServerError(req.getRequestURL().toString(),ex); 
    } 

} 

내가 JSON 형식으로 반환 할 SERVERERROR :

public class ServerError { 

    public final String url; 
    public final String error; 

    public ServerError(String url, Exception ex) { 
     this.url = url; 
     this.error = ex.getMessage(); 
    } 

    public String getUrl() { 
     return url; 
    } 

    public String getError() { 
     return error; 
    } 
} 

는 그래서 <xml>hello</xml>이 잘 반환하지만 true에 플래그를 설정하면 내가

ERROR 2017-10-18 12:56:53,189 [http-nio-0.0.0.0-8080-exec-2] org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Failed to invoke @ExceptionHandler method: eu.openminted.registry.core.exception.ServerError eu.openminted.registry.service.UserController.malformedExeption(javax.servlet.http.HttpServletRequest,java.lang.Exception) 
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 

를 얻을 수 또한 produces을 XML과 JSON으로 설정하면 동일한 결과가 반환됩니다.

@RequestMapping(value = "/test", 
     method = RequestMethod.GET, 
     produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_UTF8_VALUE}) 

답변

1

내가 원하는 유형이 그 솔루션을

@RequestMapping(value = "/test", method = RequestMethod.GET) 
public ResponseEntity<String> throwException(
    @RequestParam(value = "flag", defaultValue = "false") Boolean flag 
) throws Exception { 
    if (flag) { 
     throw new Exception(); 
    } else { 
     ResponseEntity response = ResponseEntity.ok(). 
      contentType(MediaType.APPLICATION_XML). 
      body("<xml>hello</xml>"); 
     return response; 
    } 
} 

문제를 반환 @RequestMapping에서 produces을 제거하고 ResponseEntity로 지정하여이 방법을 모두 가지고 있다는 것입니다 해결하기 위해 운영 한 그들이 생산하는 유형의 @annotation은 이것이 균등성을 파괴하지 않습니다.

+0

, 당신은 당신의 자신의 대답을 사람들 때문에 받아 들여야하는 당신의 질문은 그것이 해결되고 따라서 도움이 될 수 있다는 것을 알게 될 것입니다. – araknoid

+0

@araknoid 내가 받아 들일 수 있기까지 2 일의 대기 기간이 있습니다. – stevengatsios

0

당신은 당신의 pom.xml에 의존성 아래에 추가 할 필요가 있으며, = 생산 MediaType.APPLICATION_XML_VALUE와 코드와 함께 작동합니다 당신이 문제를 해결하기 때문에

<dependency> 
     <groupId>com.fasterxml.jackson.dataformat</groupId> 
     <artifactId>jackson-dataformat-xml</artifactId> 
    </dependency> 
+0

최소한의 예제에서는 XML을 직렬화하지 않았습니다. 이유가 무엇입니까? – stevengatsios

+0

ServerError 객체를 XML로 변환하여 직렬화하려고 했으므로 위의 종속성은 XML로 보낼 때이 객체를 직렬화 처리합니다. –

+0

하지만 JSON 형식으로 전송해야합니다. – stevengatsios

관련 문제