[英]Using Spring-Boot-Starter-Web in combination with ObjectDB
[英]"Could not find acceptable representation" using spring-boot-starter-web
我正在嘗試使用spring-boot-starter-web
創建一個 rest 服務,服務於 Java 對象的 JSON 表示。 據我了解,這個 boot-starter-web jar 應該自動處理到 JSON 到 Jackson 的轉換,但我卻收到了這個錯誤。
{
"timestamp": 1423693929568,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation"
}
我的Controller是這個...
@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 數據綁定,因此應該在類路徑上......
[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
...但調用test
服務會出現上述錯誤。 test2
和test3
工作正常,證明一定是嘗試轉換為 JSON 失敗了? 我是否遺漏了一些配置問題或注釋? 從我能找到的所有示例中,不再需要將 class 注釋為基本的 Jackson JSON 轉換。
非常感謝任何幫助。
您的 UpdateResult 沒有公共 getter,例如:
public static class UploadResult {
private String value;
public UploadResult(final String value)
{
this.value = value;
}
public String getValue() {
return this.value;
}
}
我相信默認情況下自動發現是開啟的,並且會嘗試發現你的吸氣劑。 您可以使用@JsonAutoDetect(getterVisibility=Visibility.NONE)
禁用它,並且在您的示例中將導致[]
。
我在使用spring/jhipster
RESTful 服務時遇到了類似的錯誤(通過Postman
)
端點類似於:
@RequestMapping(value = "/index-entries/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<IndexEntry> getIndexEntry(@PathVariable Long id) {
我試圖通過Postman
使用標頭Accept: text/plain
調用restful
端點,但我需要使用Accept: application/json
我也面臨着類似的問題。 在我的情況下,請求路徑接受郵件 ID 作為路徑變量,所以 uri 看起來像 /some/api/test@test.com
並且基於路徑,Spring 確定 uri 是獲取一些擴展名為“.com”的文件,並嘗試使用不同的媒體類型進行響應,然后是預期的。 在將路徑變量轉換為請求參數后,它對我有用。
如果使用@FeignClient,添加例如
produces = "application/json"
到 @RequestMapping 注釋
如果您使用的是Lombok ,請確保它在您的響應模型類中有@Data 或 @Getter @Setter 之類的注釋。
將以下依賴項添加到您的pom.xml
:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.2</version>
</dependency>
我在訪問執行器時遇到了這個問題。 放置以下配置類解決了這個問題:
@Configuration
@EnableWebMvc
public class MediaConverterConfiguration implements WebMvcConfigurer {
@Bean
public MappingJackson2HttpMessageConverter jacksonConverter() {
MappingJackson2HttpMessageConverter mc =
new MappingJackson2HttpMessageConverter();
List<MediaType> supportedMediaTypes =
new ArrayList<>(mc.getSupportedMediaTypes());
supportedMediaTypes
.add(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));
supportedMediaTypes.add(
MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json"));
mc.setSupportedMediaTypes(supportedMediaTypes);
return mc;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonConverter());
}
}
我的返回對象在類上沒有 @XmlRootElement 注釋。 添加注釋解決了我的問題。
@XmlRootElement(name = "ReturnObjectClass")
public class ReturnObjectClass {
@XmlElement(name = "Status", required = true)
protected StatusType status;
//...
}
我遇到了完全相同的問題。 查看此參考后: https ://zetcode.com/springboot/requestparam/
我的問題通過改變解決了
method = RequestMethod.GET, produces = "application/json;charset=UTF-8"
至
method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE
並且不要忘記添加庫:
import org.springframework.http.MediaType;
它適用於郵遞員或常規瀏覽器。
就我而言,我在 ResponseEntity<>() 中發送了正確的對象。
正確的 :
@PostMapping(value = "/get-customer-details", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> getCustomerDetails(@RequestBody String requestMessage) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("customerId", "123");
jsonObject.put("mobileNumber", "XXX-XXX-XXXX");
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.OK);
}
不正確:
return new ResponseEntity<>(jsonObject, HttpStatus.OK);
因為,jsonObject 的 toString() 方法“將 jsonObject 編碼為緊湊的 JSON 字符串”。 因此,Spring 直接返回 json 字符串,無需任何進一步的序列化。
當我們改為發送 jsonObject 時,Spring 會嘗試基於 RequestMapping 中使用的 producer 方法對其進行序列化,但由於“找不到可接受的表示”而失敗。
我必須在我的 POM 中顯式調用我的 json 庫的依賴項。
一旦我添加了以下依賴項,這一切都奏效了。
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
就我而言,我碰巧正在使用 lombok,顯然與 get 和 set 存在沖突
我也有同樣的例外。 問題是,我使用了注釋
@RepositoryRestController
代替
@RestController
在要反序列化的pojo類上添加@Data注釋。 問題解決了我:)
例如,
import lombok.Data;
@Data
public class Response {
private Integer status;
private String type;
}
當我的一個控制器使用空@GetMapping
攔截所有請求時遇到了類似的問題
對我來說,問題是試圖在 url 中傳遞一個帶點的文件名。 例如
"http://localhost:8080/something/asdf.jpg" //causes error because of '.jpg'
它可以通過不傳遞 .jpg 擴展名或將其全部發送到請求正文中來解決。
我遇到了同樣的問題,我在設置中缺少的是將它包含在我的 maven (pom.xml) 文件中。
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency>
如果您使用郵遞員進行測試,請嘗試簽出標題。 我的錯誤是我在“接受”行中使用 application/xml 而不是 application/json。
花了一整天的時間,因為沒有一個建議對我有用。 las,這是WebConfig
的實現; 特別是configurer.ignoreAcceptHeader(true)
是阻止.yaml
生成的罪魁禍首。
希望這對某人有幫助。
我遇到了同樣的異常:“已解決[org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示”]
在分析中,我發現這個問題可能在調用 @Controller 或 @RestController 時序列化 class 時產生。
在序列化中,如果你使用 class,它必須有 public Setter 和 Getter 。
在它發生的項目中,我發現一個 getter 方法是私有的,因此發生了這個問題。
Spring 5 接受的答案不正確。嘗試將 Web 服務的 URL 更改為 .json! 這是正確的解決方法。 這里有很多細節http://stick2code.blogspot.com/2014/03/solved-orgspringframeworkwebhttpmediaty.html
我有同樣的例外,但原因不同,我找不到任何信息,所以我會在這里發布。 這只是一個容易忽略的錯誤。
壞的:
@RestController(value = "/api/connection")
好的:
@RestController
@RequestMapping(value = "/api/connection")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.