【发布时间】:2019-12-06 13:39:07
【问题描述】:
我的 .NET 应用程序中有这个类,用于将一些数据从客户端(.NET)发送到服务器(Spring):
class NetworkController
{
private static readonly HttpClient client = new HttpClient();
public static async Task SendUserDataAsync()
{
var values = new Dictionary<string, string>
{
{ "firstName", "sunny" },
{ "lastName", "leone" },
{ "timeStamp", "test" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://localhost:8080/user", content);
var responseString = await response.Content.ReadAsStringAsync();
}
}
在我的 Spring Boot 应用程序中,我有一个名为 User 的类:
@Entity
public class User
{
@Id
private String firstName;
private String lastName;
private String timeStamp;
public User(){}
@Override
public String toString() {
return "firstName : "+this.firstName + "\n"+"lastName : " + this.lastName;
}
}
在我的 rest-controller 我有这个方法可以插入 User :
@PostMapping("/user")
User addUser(@RequestBody User user)
{
System.out.println(user);//this always prints an empty line, maybe receiving nothing
return userRepository.save(user);
}
我收到此警告已解决 [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
我在 .NET 中创建了这个类(带有 Spring 的概念),但似乎没有用:
class User
{
String firstName;
String lastName;
String timeStamp;
public User()
{
firstName = "1"
lastName = "2"
timeStamp = "test"
}
}
发送一个对象而不是字典不是更温和整洁吗?怎么办?
我该如何解决这个问题?
【问题讨论】:
-
似乎发送字典时它没有序列化为json。这个答案建议使用 Json.NET 将对象序列化为 json -- stackoverflow.com/questions/36625881/…
标签: java .net spring spring-boot dotnet-httpclient