我有一个简单的服务器,该服务器应具有与数据库一起使用的POST,PUT和GET方法。我有一个应用程序,在这里我使用Retrofit2,在我使用GET调用时-没关系,我可以从数据库中获取数据。但是,当我尝试使用POST或PUT创建新数据或更新数据库时,我的服务器没有收到来自应用程序的任何调用。 请向我解释我错了。
RemindController.java:
@RestController
public class ReminderController {
@Autowired
private RemindRepository repository;
@RequestMapping(value = "/profile", method = RequestMethod.GET)
@ResponseBody
public List<Profile> getAllProfiles( ){
return repository.findAll();
}
@RequestMapping(value = "/profile/{id}", method = RequestMethod.GET)
@ResponseBody
public Profile getProfile(@PathVariable("id") long profileID){
return repository.findById(profileID).get();
}
@RequestMapping(value = "/profile", method = RequestMethod.POST)
@ResponseBody
public Profile saveProfile(@RequestBody Profile profile){
return repository.saveAndFlush(profile);
}
@RequestMapping(value = "/profile/{id}", method = RequestMethod.PUT)
@ResponseBody
public Profile putProfile(@PathVariable("id") long id, @RequestParam String name, @RequestParam String address){
Profile profile = repository.findById(id).get();
profile.setCourier_name(name);
profile.setAddress(address);
repository.save(profile);
return profile;
}
}
实体类:
@Entity
@Table(name = "profile")
public class Profile {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;
@Column(name = "courier_name", nullable = false, length = 50)
private String courier_name;
@Column(name = "address", nullable = false, length = 100)
private String address;
@Column(name = "number", nullable = false, length = 10)
private String number;
@Column(name = "info", nullable = false, length = 100)
private String info;
public Profile() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCourier_name() {
return courier_name;
}
public void setCourier_name(String courier_name) {
this.courier_name = courier_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
我拨打电话的代码(POST / PUT):
...
...
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.2:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
mSaveProfile = findViewById(R.id.save_data_profile);
mSaveProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeCallPost();
// makeCallPut();
}
});
// makeCallGet();
}
public void makeCallPut(){
Call<GetAndSetProfile> call = jsonPlaceHolderApi.putProfile(1, "Someone", "Berlin");
call.enqueue(new Callback<GetAndSetProfile>() {
@Override
public void onResponse(Call<GetAndSetProfile> call, Response<GetAndSetProfile> response) {
if (!response.isSuccessful()){
Toast toast = Toast.makeText(getApplicationContext(),
"Error 404", Toast.LENGTH_LONG);
toast.show();
}
}
@Override
public void onFailure(Call<GetAndSetProfile> call, Throwable t) {
mName.setText(t.getMessage());
Toast toast = Toast.makeText(getApplicationContext(),
"Error", Toast.LENGTH_LONG);
toast.show();
}
});
}
public void makeCallPost(){
GetAndSetProfile getAndSetProfile = new GetAndSetProfile(1, mProfile.getName(), mProfile.getAddress(),
mProfile.getNumber(), mProfile.getInfo());
Call<GetAndSetProfile> call = jsonPlaceHolderApi.saveProfile(getAndSetProfile);
call.enqueue(new Callback<GetAndSetProfile>() {
@Override
public void onResponse(Call<GetAndSetProfile> call, Response<GetAndSetProfile> response) {
if (!response.isSuccessful()){
Toast toast = Toast.makeText(getApplicationContext(),
"Error 404", Toast.LENGTH_LONG);
toast.show();
}
Toast toast = Toast.makeText(getApplicationContext(),
"Saved", Toast.LENGTH_LONG);
toast.show();
return;
}
@Override
public void onFailure(Call<GetAndSetProfile> call, Throwable t) {
mName.setText(t.getMessage());
Toast toast = Toast.makeText(getApplicationContext(),
"Error", Toast.LENGTH_LONG);
toast.show();
}
});
}
GetAndSetProfile.java
public class GetAndSetProfile {
@SerializedName("increment")
private long id;
private String courier_name;
private String address;
private String number;
private String info;
public GetAndSetProfile(long id, String courier_name, String address, String number, String info) {
this.id = id;
this.courier_name = courier_name;
this.address = address;
this.number = number;
this.info = info;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCourier_name() {
return courier_name;
}
public void setCourier_name(String courier_name) {
this.courier_name = courier_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
JsonPlaceHolderApi.java
public interface JsonPlaceHolderApi {
@GET("profile/{id}")
Call<GetAndSetProfile> getAllProfiles(@Path("id") long id);
@POST("profile")
Call<GetAndSetProfile> saveProfile(@Body GetAndSetProfile getAndSetProfile);
@FormUrlEncoded
@PUT("profile/{id}")
Call<GetAndSetProfile> putProfile(@Path("id") long id, @Field("name") String name, @Field("address") String address);
}
如何使服务器处理POST和Put呼叫? 我已经学习编程仅两个星期了,我不了解很多事情。所有这些代码,我都来自不同的资源,并不完全理解。 希望得到您的帮助,并先谢谢您。