最终使用Android应用程序与我的aws服务器和数据库进行通信,我可以使用postman和GET服务本地与数据库进行通信,但是现在我正在尝试将我的spring应用程序更改为POST并使用android studio中的对象需要这样我可以将我的应用程序加载到云端并从android
运行哪些线路需要改变,我对此完全陌生,并尝试学习,因为我同意这一点。
主控制器类
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private ImageRepository imageRepository;
@RequestMapping(path = "/add") // Map ONLY GET Requests
public @ResponseBody String addNewImages (@RequestParam String word
, @RequestParam String image, @RequestParam String category,
@RequestParam int number, @RequestParam String users_username) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
images i = new images();
i.setWord(word);
i.setImage(image.getBytes());
i.setCategory(category);
i.setNumber(number);
i.setUsers_username(users_username);;
//imageRepository.save(i);
return "Saved";
}
@PostMapping(path = "/add")
public String addsubmit(@ModelAttribute images img){
imageRepository.save(img);
return "saved";
}
@GetMapping(path="/all")
public @ResponseBody
Iterable<images> getAllImages() {
// This returns a JSON or XML with the users
return imageRepository.findAll();
}
}
图像类
public images(String word, byte[] image, String category, int number, String users_username) {
this.word = word;
this.image = image;
this.category = category;
this.number = number;
this.users_username = users_username;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getUsers_username() {
return users_username;
}
public void setUsers_username(String users_username) {
this.users_username = users_username;
}
答案 0 :(得分:0)
也许您只需更改Requestmethod?
@RequestMapping(path = "/add", method = RequestMethod.POST)
答案 1 :(得分:0)
要更改您必须执行以下操作的请求类型:
@RequestMapping(path="/demo", method=RequestMethod.POST)
如果要将对象(例如,JSON)发布到端点,则将此对象作为方法参数发送,并使用@RequestBody注释它
public @ResponseBody String addNewImages (@RequestBody Object body) {}