0

I have the following setup:

My controller:

@RequestMapping("/project")
@RestController
public class ProjectController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ProjectService projectService;

    @CrossOrigin
    @PostMapping(value = "/createProject")
    public ResponseEntity createProject(@RequestBody ProjectDto projectJsonString) {
        return ResponseEntity.ok(HttpStatus.OK);
    }
}

In my .service.ts:

this.http.post('http://localhost:8080/project/createProject', JSON.stringify(project)).
      subscribe( (res) => {
        this.logger.info('Response: ' + res);
      });

My Dto:

public class ProjectDto {
    private String projectName;
    private String projectNumber;
    private String projectArea;
    private String managerName;
    private String managerShorthand;
}

But when I build the app to a jar-file and execute it I get the following error when the api-call is executed:

HttpErrorResponse {headers: HttpHeaders, status: 415, statusText: "OK", url: "http://localhost:8080/project/createProject", ok: false, …}
error:
error: "Unsupported Media Type"
message: "Content type 'text/plain;charset=UTF-8' not supported"
path: "/project/createProject"
status: 415
timestamp: "2018-12-10T21:41:26.036+0000"

and the same thing happens when I curl it. Can someone tell me what I am doing wrong?

4
  • What is ‘createProject’ in the url? I don’t see mapping for that. Just use localhost:8080/project Commented Dec 10, 2018 at 21:33
  • @Lemmy Sorry, I see my class still contains the const for that url, I will update it. It is the mapper of my method - I saw the first problem, I used name instead of value, thanks for that! Commented Dec 10, 2018 at 21:39
  • 1
    I understood that but I am saying that you don’t have mapping for createProject. Use value instead of name. see docs.spring.io/spring-framework/docs/current/javadoc-api/org/… Commented Dec 10, 2018 at 21:44
  • 1
    remove stringify because you have to send json Commented Dec 10, 2018 at 21:57

2 Answers 2

1

The error "Unsupported Media Type" happens when the request does not include an appropriate Content-Type header.

Please make sure your requests contains "Content-Type" header with the value "application/json" (assuming your sent data is in Json).

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned by HL'REB, you should add the correct Content-Type to your post request, as you are using spring boot and RestControllers I Assume the default json support is activated so probably this is the Media Type expected(application/json).

So you could try the following change

this.http.post('http://localhost:8080/project/createProject', JSON.stringify(project), 
{ 
  headers: {'Content-Type': 'application/json'} 
}).subscribe( (res) => {this.logger.info('Response: ' + res);});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.