Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order Controller /cancel Mapping Feedback #106

Closed
sidneyrens opened this issue Aug 23, 2022 · 2 comments
Closed

Order Controller /cancel Mapping Feedback #106

sidneyrens opened this issue Aug 23, 2022 · 2 comments
Assignees

Comments

@sidneyrens
Copy link

Hello! I just completed your tutorial and I am so grateful for your contribution! It helped me greatly in my Spring studies.

I am not sure if you still actively manage this repository/tutorial, but I wanted to call out that the use of Problem.create() did not work for me in my code. I am not sure if I was implementing something incorrectly, but Intellij could not find a create() method on a Problem class, and I couldn't find it on the Problem class on the Spring documentation

Maybe I am missing something, but what ended up working for me was:

    @DeleteMapping("/orders/{id}/cancel")
    ResponseEntity<?> cancel(@PathVariable Long id) {
      Order order = orderRepository
              .findById(id)
              .orElseThrow(() -> new OrderNotFoundException(id));

      if(order.getStatus() == Status.IN_PROGRESS) {
          order.setStatus(Status.CANCELLED);
          return ResponseEntity.ok(assembler.toModel(orderRepository.save(order)));
      }

        return ResponseEntity
                .status(HttpStatus.METHOD_NOT_ALLOWED)
                .header(HttpHeaders.CONTENT_TYPE, MediaTypes.HTTP_PROBLEM_DETAILS_JSON_VALUE)
                .body("You can't cancel an order that is in the " + order.getStatus() + " status");
    }

Obviously I am just learning, I am not sure if this would be a preferred method in a real-world example. I'd love feedback (from anyone) if someone takes a look.

Again, thank you so much! <3

@AAdewunmi
Copy link

Hi @sidneyrens, I had the same problem using Eclipse STS, then I googled the error and found a solution:

java.lang.IllegalArgumentException: Invalid character found in method name [0x160x030x010x000xf70x010x000x000xf30x030x03H0x160xe30xc9Z0x1cr0xc00xc60x7f0x05Y0x08[0x150xa70xaf0xa70xdc0x1b0xf9'0xa5q0x8d;0xe10xd20x180xca0x840xe8 ]. HTTP method names must be tokens

It has to do with using https / http ->

Employee API makes the following call:

$ curl -v localhost:8080/employees

or

$ curl -v https://localhost:8080/employees

Orders API makes the following call:

$ curl -v http://localhost:8080/orders

Essentially, swapping "http" with "https" will fix the problem.

😎

@robertmcnees robertmcnees self-assigned this May 15, 2024
@robertmcnees
Copy link
Contributor

Hi @sidneyrens. Thank you for the issue. I think I see the problem.

The Problem class you linked to comes from the Spring Framework project. You are correct that this class does not have a create() method, so that code would cause an error.

The correct Problem class should be the one that comes from the Spring HATEOAS project. This one does have an implementation of create().

While the code has the correct implementation in the form of the import:
import org.springframework.hateoas.mediatype.problem.Problem;
The text in the README doesn't call out the imports explicitly, leaving it to the user and their IDE to infer.

I will close the issue for now as I don't think it is a bug. If others have the same issue we can revisit the README and add the import explicitly like we've done in other situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants