[SPRING] spring-boot-startter-web을 사용하여 "수용 가능한 표현을 찾을 수 없습니다"
SPRINGspring-boot-startter-web을 사용하여 "수용 가능한 표현을 찾을 수 없습니다"
Java 객체의 JSON 표현을 제공하는 휴식 서비스를 만들기 위해 spring-boot-startter-web을 사용하려고합니다. 이 boot-starter-web jar가 Jackson을 통해 JSON으로의 변환을 자동으로 처리하기로되어 있지만이 오류가 발생하는 것으로 알고 있습니다.
{ "timestamp": 1423693929568,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation"
}
내 컨트롤러가 ...
@RestController
@RequestMapping(value = "/media")
public class MediaController {
@RequestMapping(value = "/test", method = RequestMethod.POST)
public @ResponseBody UploadResult test(@RequestParam(value="data") final String data) {
String value = "hello, test with data [" + data + "]";
return new UploadResult(value);
}
@RequestMapping(value = "/test2", method = RequestMethod.POST)
public int test2() {
return 42;
}
@RequestMapping(value = "/test3", method = RequestMethod.POST)
public String test3(@RequestParam(value="data") final String data) {
String value = "hello, test with data [" + data + "]";
UploadResult upload = new UploadResult(value);
return upload.value;
}
public static class UploadResult {
private String value;
public UploadResult(final String value)
{
this.value = value;
}
}
}
내 pom.xml 있습니다 ...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.1.RELEASE</version>
<scope>provided</scope>
</dependency>
mvn dependency : tree는 spring-boot-starter-web이 실제로 jackson2.4 데이터 바인딩에 의존하므로 classpath에 있어야한다고 보여줍니다.
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.2.1.RELEASE:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.8:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.8:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.14:runtime
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.4:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.4.4:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] | +- org.springframework:spring-web:jar:4.1.4.RELEASE:compile
[INFO] | | +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile
[INFO] | | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | | +- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:4.1.4.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.1.4.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
... 아직 테스트 서비스를 호출하면 위에서 언급 한 오류가 발생합니다. test2와 test3은 실패한 JSON으로의 변환을 시도해야한다는 것을 증명합니다. 구성 문제 나 주석이 누락 되었습니까? 찾을 수있는 모든 예제에서 기본적인 Jackson JSON 변환에 대한 클래스에 주석을 달아주는 작업은 더 이상 필요하지 않습니다.
어떤 도움이라도 대단히 감사합니다.
해결법
-
==============================
1.UpdateResult에 대한 공개 getter가 없습니다. 예를 들면 다음과 같습니다.
UpdateResult에 대한 공개 getter가 없습니다. 예를 들면 다음과 같습니다.
public static class UploadResult { private String value; public UploadResult(final String value) { this.value = value; } public String getValue() { return this.value; } }
나는 기본적으로 자동 검색이 켜져 있고 getters를 발견하려고 할 것이라고 믿는다. @JsonAutoDetect (getterVisibility = Visibility.NONE)로 비활성화 할 수 있으며, 예제에서는 []가됩니다.
-
==============================
2.비슷한 오류가 봄 / jhipster RESTful 서비스를 사용하여 (우편 배달부를 통해)
비슷한 오류가 봄 / jhipster RESTful 서비스를 사용하여 (우편 배달부를 통해)
끝점은 다음과 같습니다.
@RequestMapping(value = "/index-entries/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<IndexEntry> getIndexEntry(@PathVariable Long id) {
나는 Accept : text / plain 헤더를 가진 Postman을 통해 편안한 엔드 포인트를 호출하려고 시도했지만 Accept를 사용해야했습니다 : application / json
-
==============================
3.나도 비슷한 문제에 직면했다. 제 경우에는 요청 경로가 메일 변수를 경로 변수로 받아들이므로 URI가 다음과 같이 보입니다. /some/api/test@test.com
나도 비슷한 문제에 직면했다. 제 경우에는 요청 경로가 메일 변수를 경로 변수로 받아들이므로 URI가 다음과 같이 보입니다. /some/api/test@test.com
그리고 경로를 기반으로, Spring은 uri가 ".com"확장자로 파일을 가져오고, 응답을 위해 다른 미디어 유형을 사용하려한다는 것을 결정했습니다. 요청 매개 변수로 경로 변수를 만든 후 그것은 나를 위해 일했습니다.
-
==============================
4.@FeignClient를 사용하는 경우
@FeignClient를 사용하는 경우
produces = "application/json"
@RequestMapping 주석으로
-
==============================
5.반환 객체에는 클래스에 @XmlRootElement 주석이 없습니다. 특수 효과를 추가하면 내 문제가 해결되었습니다.
반환 객체에는 클래스에 @XmlRootElement 주석이 없습니다. 특수 효과를 추가하면 내 문제가 해결되었습니다.
@XmlRootElement(name = "ReturnObjectClass") public class ReturnObjectClass { @XmlElement(name = "Status", required = true) protected StatusType status; //... }
-
==============================
6.내 POM에서 내 json 라이브러리에 대한 종속성을 명시 적으로 호출해야했습니다.
내 POM에서 내 json 라이브러리에 대한 종속성을 명시 적으로 호출해야했습니다.
아래의 의존성을 추가하면 모두 작동합니다.
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
-
==============================
7.대답은 Spring 5에서 올바르지 않습니다. 웹 서비스의 URL을 .json으로 변경해보십시오! 그것은 올바른 수정입니다. 여기에서 훌륭한 세부 정보 http://stick2code.blogspot.com/2014/03/solved-orgspringframeworkwebhttpmediaty.html
대답은 Spring 5에서 올바르지 않습니다. 웹 서비스의 URL을 .json으로 변경해보십시오! 그것은 올바른 수정입니다. 여기에서 훌륭한 세부 정보 http://stick2code.blogspot.com/2014/03/solved-orgspringframeworkwebhttpmediaty.html
-
==============================
8.의존성 아래에 추가 :
의존성 아래에 추가 :
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.4.6</version> </dependency>
from https://stackoverflow.com/questions/28466207/could-not-find-acceptable-representation-using-spring-boot-starter-web by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] <context : include-filter>와 <context : exclude-filter>는 Spring에서 어떻게 작동합니까? (0) | 2019.02.01 |
---|---|
[SPRING] 스프링 MVC : 경로 변수가 필요한지 여부를 나타내는 방법? (0) | 2019.02.01 |
[SPRING] JPA는 분리 된 객체를 삭제한다고 생각합니다. (0) | 2019.02.01 |
[SPRING] @Component로 클래스에 주석을 달 때 Spring Bean과 Singleton을 의미합니까? (0) | 2019.02.01 |
[SPRING] 어떻게 봄을 사용하여 다시 브라우저 버튼 문제를 처리 할 수 있습니까? (0) | 2019.02.01 |