簡體   English   中英

警告:已解決 [org.springframework.web.HttpMediaTypeNotSupportedException:不支持內容類型 'application/json']

[英]WARNING: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported]

在此處輸入圖像描述 在此處輸入圖像描述 在此處輸入圖像描述 大家好,請告訴我如何解決這個問題我正在嘗試使用 rest web 服務,請參閱下面的代碼


import java.sql.ResultSet;
import java.util.Date;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cwg.entrust.entities.InputString;
import com.cwg.entrust.entities.TokenObject;
import com.cwg.entrust.services.MyConnectionProvider;
import com.cwg.entrust.services.TolenDAO;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;[![enter image description here][3]][3]
@RestController
@RequestMapping("/test")
public class TfaServiceController implements TolenDAO {
    java.sql.Connection con = null;
    java.sql.PreparedStatement ps = null;
    Date date = new Date();
    String the_date = date.toString();
@PostMapping(value = { "/validateToken" }, 
            consumes = { MediaType.APPLICATION_JSON_VALUE }
    , produces = { MediaType.APPLICATION_JSON_VALUE })
    public Boolean getIsValidToken(@RequestBody Map<String, String> json_Input) throws Exception {
        String str = json_Input.get("str1") ;
        String token = json_Input.get("str2") ;

        Boolean result = false;

        if (str.equalsIgnoreCase(token)) {
            result = true;
        }

        return result;
    }

}

請參閱下面的有效載荷

{
    "str1": "test one",
    "str2": "two test"
}

Postman 上的 Content-Type 和 Accept 都設置為application/json

但我們一直低於錯誤

警告:已解決 [org.springframework.web.HttpMediaTypeNotSupportedException:不支持內容類型 'application/json']

請建議我如何毫無問題地使用此網絡服務謝謝大家。

Since you are using the @RestController annotation at the class level, you do not have to use the @ResponseBody annotation at the method level to indicate that it is a Rest endpoint and that the response type of the controller method will be a json (by springboot 中 Rest 的默認值)。將 controller 方法更改為:

@RequestMapping(value = "/validateToken", method = RequestMethod.POST)
public Boolean getIsValidToken(@RequestBody Map<String, String> json_Input) throws Exception {
    String str = json_Input.get("str1") ;
    String token = json_Input.get("str2") ;

    Boolean result = false;

    if (str.equalsIgnoreCase(token)) {
        result = true;
    }

    return result;
}

如果您的 springboot 版本支持,您也可以使用@PostMapping("/validateToken")注釋,而不是@RequestMapping(value = "/validateToken", method = RequestMethod.POST)除此之外,您可以指定類型端點期望或產生的數據使用如下:

@PostMapping(value = { "/validateToken" }, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE )
public Boolean getIsValidToken(@RequestBody Map<String, String> json_Input) throws Exception {
    String str = json_Input.get("str1") ;
    String token = json_Input.get("str2") ;

    Boolean result = false;

    if (str.equalsIgnoreCase(token)) {
        result = true;
    }

    return result;
}

注意:消費/生產也可以與@RequestMapping一起使用。

這個問題也發生在我身上。 當我在屬性名稱(在您的情況下為 json_Input)之前引入類型 String 時它起作用,而當我嘗試使用我的自定義類型時它不起作用。

最后是我引用了屬性的類型而不是字符串類型(因為我使用的是 mongodb)。

所以,而不是

@DBRef
private CustomType customProperty

利用:

@DBRef
private String customProperty

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM