java.lang.illegalargumentexception:在方法名称中找到无效字符在spring和android中,http方法名必须是令牌

j9per5c4  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(254)

我正在尝试将我的springapi与android集成。但是,我面临着这个错误。

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)

我尝试在我的应用程序中将https更改为http retrofit 类(在android项目中),但它不起作用。当我试着在 Postman ,没有错误。请看一下我的密码。
来自spring应用程序的api

@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);
        }
    }

从android应用程序升级服务

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);
    }
}

来自android应用程序的接口api

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

来自android应用程序的用户存储库

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;
    }
}

android应用程序的userviewmodel

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;
    }
}

有人对如何使它起作用有什么建议吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题