嗨,大家好,请为我提供有关如何解决此问题的建议
我正在尝试使用其余的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"
}
Content-Type和Postman上的Accept都设置为application/json
但是我们一直在错误以下
警告:已解决 [org.springframework.web.HttpMediaTypeNotSupportedException:内容 类型'application / json'不支持]
请为我提供有关如何正常使用此Web服务的建议 谢谢大家。
答案 0 :(得分:0)
由于在类级别使用@RestController
批注,因此不必在方法级别使用@ResponseBody
批注来表明它是Rest终结点并且响应类型为控制器方法将为json
(在springboot中默认为Rest)。将控制器方法更改为:
@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版本支持@RequestMapping(value = "/validateToken", method = RequestMethod.POST)
,也可以使用@PostMapping("/validateToken")
注释,除此之外,还可以使用以下命令指定端点期望或产生的数据类型: / p>
@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
一起使用。