2020年12月22日星期二

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens in Spring & Android

I'm trying to integrate my Spring API with Android. But, I'm facing this error.

Dec 23, 2020 5:52:39 AM org.apache.coyote.http11.Http11Processor service  INFO: Error parsing HTTP request header   Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.  java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens      at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:414)      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294)      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)      at java.base/java.lang.Thread.run(Thread.java:832)

I've tried change HTTPS to HTTP in my retrofit class (in Android project) but it doesn't work. While when I try in Postman, There's no error. Please take a look at my code.

API from Spring application

@PostMapping("/register")      public ResponseEntity<?> register (@RequestBody User nb) {          try {              send.regisUser(new Gson().toJson(nb));              recv.receiveFromDB();              JSONObject object = new JSONObject();              object.put("response",200);              object.put("status","Success");              object.put("message","Success");              return new ResponseEntity<>(object, HttpStatus.OK);          } catch (Exception e) {              System.out.println("ERROR REGISTER: " + e);              JSONObject object = new JSONObject();              object.put("response",400);              object.put("status","Error");              object.put("message","Error");              return new ResponseEntity<>(object, HttpStatus.OK);          }      }

RetrofitService from Android application

public class RetrofitService {      private static Retrofit retrofit = new Retrofit.Builder()              .baseUrl("http://<IP>/")              .addConverterFactory(GsonConverterFactory.create())              .build();        public static <S> S createService(Class<S> serviceClass) {          return retrofit.create(serviceClass);      }  }

Interface API from Android application

public interface API {      @POST("/register")      Call<APIResponse> newUser(@Body User body);  }

User Repository from Android application

public class UserRepository {      public static UserRepository nbRepo;      private API api;        public static UserRepository getInstance() {          if (nbRepo == null) {              nbRepo = new UserRepository();          }          return nbRepo;      }        public UserRepository() {          API = RetrofitService.createService(API.class);      }        public MutableLiveData<APIResponse> doRegister(User nb) {          String req = new Gson().toJson(nb);          System.out.println("Register request: " + req);          MutableLiveData<APIResponse> result = new MutableLiveData<>();          API.newUser(nb).enqueue(new Callback<APIResponse>() {                @Override              public void onResponse(Call<APIResponse> call, Response<APIResponse> responses) {                  result.setValue(responses.body());                  System.out.println("Isi response register: " + responses.body());              }                @Override              public void onFailure(Call<APIResponse> call, Throwable t) {                  System.out.println("Error register");              }          });          return result;      }  }

UserViewModel from Android application

public class UserViewModel extends ViewModel {      private UserRepository nbRepo;      private LiveData<APIResponse> liveData;        public void init() {          if (liveData!=null) {              return;          }          nbRepo = nbRepo.getInstance();      }        public LiveData<APIResponse> registrasi(User regis) {          if (liveData == null) {              nbRepo = nbRepo.getInstance();          }          liveData = nbRepo.doRegister(regis);          return liveData;      }  }

Do anyone have any suggestion how to make it works?

https://stackoverflow.com/questions/65417005/java-lang-illegalargumentexception-invalid-character-found-in-method-name-http December 23, 2020 at 07:07AM

没有评论:

发表评论